locked
how to add namespace in XMLDocument RRS feed

  • Question

  • Hi,

    I have a XML document as:

    <?xml version="1.0" encoding="UTF-8" ?>
    - <Employees>
    - <Employee>
      <ID>1</ID>
      <Name>A</Name>
      <Salary>10000</Salary>
      </Employee>
    - <Employee>
      <ID>2</ID>
      <Name>B</Name>
      <Salary>20000</Salary>
      </Employee>
    - <Employee>
      <ID>3</ID>
      <Name>C</Name>
      <Salary>30000</Salary>
      </Employee>
    - <Employee>
      <ID>4</ID>
      <Name>D</Name>
      <Salary>40000</Salary>
      </Employee>

    -<Employee>

      </Employees>

    i want to add the following Namespace in my XML

    xmlns:ns0="http://BiztalkProjectPOC.SourceSchema"

    I created the XML by XmlDocument using C# .

    please suggest me .

    Thanks

    Wednesday, July 16, 2014 9:01 AM

Answers

  • Hello,

    >>i want to add the following Namespace in my XML

    If you use CreateElement() method to create your node, you can use its thirty overridden method:

    public virtual XmlElement CreateElement(string prefix, string localName, string namespaceURI);

    Here is an example:

    XNamespace ns0 = "http://BiztalkProjectPOC.SourceSchema";
    
    
                XmlDocument xmlDoc = new XmlDocument();
    
                XmlNode rootNode = xmlDoc.CreateElement("ns0", "users", ns0.NamespaceName);
    
                xmlDoc.AppendChild(rootNode);
    
    
                XmlNode userNode = xmlDoc.CreateElement("user");
    
                XmlAttribute attribute = xmlDoc.CreateAttribute("age");
    
                attribute.Value = "42";
    
                userNode.Attributes.Append(attribute);
    
                userNode.InnerText = "John Doe";
    
                rootNode.AppendChild(userNode);
    
    
                userNode = xmlDoc.CreateElement("user");
    
                attribute = xmlDoc.CreateAttribute("age");
    
                attribute.Value = "39";
    
                userNode.Attributes.Append(attribute);
    
                userNode.InnerText = "Jane Doe";
    
                rootNode.AppendChild(userNode);
    
    
                xmlDoc.Save("test-doc.xml");
    

    It would create a XML file as:

    <ns0:users xmlns:ns0="http://BiztalkProjectPOC.SourceSchema">
    
      <user age="42">John Doe</user>
    
      <user age="39">Jane Doe</user>
    
    </ns0:users>
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Thursday, July 24, 2014 2:41 AM
    Thursday, July 17, 2014 3:19 AM

All replies

  • <?xml version="1.0" encoding="UTF-8" ?>
    - <Employees>
    - <Employee>
      <ID>1</ID>
      <Name>A</Name>
      <Salary>10000</Salary>
      </Employee>
    - <Employee>
      <ID>2</ID>
      <Name>B</Name>
      <Salary>20000</Salary>
      </Employee>
    - <Employee>
      <ID>3</ID>
      <Name>C</Name>
      <Salary>30000</Salary>
      </Employee>
    - <Employee>
      <ID>4</ID>
      <Name>D</Name>
      <Salary>40000</Salary>
      </Employee>
    - <Employee>
      <ID>5</ID>
      <Name>E</Name>
      <Salary>50000</Salary>

     </Employee>

    </Employees>

    Wednesday, July 16, 2014 9:08 AM
  • <?xml version="1.0" encoding="UTF-8"?>
    <Employees>
      <Employee>
        <ID>1</ID>
        <Name>A</Name>
        <Salary>10000</Salary>
      </Employee>
      <Employee>
        <ID>2</ID>
        <Name>B</Name>
        <Salary>20000</Salary>
      </Employee>
      <Employee>
        <ID>3</ID>
        <Name>C</Name>
        <Salary>30000</Salary>
      </Employee>
      <Employee>
        <ID>4</ID>
        <Name>D</Name>
        <Salary>40000</Salary>
      </Employee>
      <Employee>

      </Employees>

    Wednesday, July 16, 2014 9:09 AM
  • Hello,

    >>i want to add the following Namespace in my XML

    If you use CreateElement() method to create your node, you can use its thirty overridden method:

    public virtual XmlElement CreateElement(string prefix, string localName, string namespaceURI);

    Here is an example:

    XNamespace ns0 = "http://BiztalkProjectPOC.SourceSchema";
    
    
                XmlDocument xmlDoc = new XmlDocument();
    
                XmlNode rootNode = xmlDoc.CreateElement("ns0", "users", ns0.NamespaceName);
    
                xmlDoc.AppendChild(rootNode);
    
    
                XmlNode userNode = xmlDoc.CreateElement("user");
    
                XmlAttribute attribute = xmlDoc.CreateAttribute("age");
    
                attribute.Value = "42";
    
                userNode.Attributes.Append(attribute);
    
                userNode.InnerText = "John Doe";
    
                rootNode.AppendChild(userNode);
    
    
                userNode = xmlDoc.CreateElement("user");
    
                attribute = xmlDoc.CreateAttribute("age");
    
                attribute.Value = "39";
    
                userNode.Attributes.Append(attribute);
    
                userNode.InnerText = "Jane Doe";
    
                rootNode.AppendChild(userNode);
    
    
                xmlDoc.Save("test-doc.xml");
    

    It would create a XML file as:

    <ns0:users xmlns:ns0="http://BiztalkProjectPOC.SourceSchema">
    
      <user age="42">John Doe</user>
    
      <user age="39">Jane Doe</user>
    
    </ns0:users>
    

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Fred Bao Thursday, July 24, 2014 2:41 AM
    Thursday, July 17, 2014 3:19 AM