Hi,

how can I create using asp.net simple xml file which looks like following:

<?xml version="1.0" encoding="UTF-8" ?> 
<my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-07-13T10:21:01">
  <my:field1>FirstName</my:pole1> 
  <my:field2>SecondName</my:pole2> 
  <my:field3 /> 
  <my:field4 /> 
  <my:field xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" /> 
  </my:myFields>

How can I write prefix my on beggining of tag?

Recommended Answers

All 4 Replies

Hope this will help you

XmlDocument xmldoc = new XmlDocument();

        xmldoc.LoadXml("<?xml version='1.0' ?>" + "<MyFields>" + "</MyFields>");
        XmlNode root = xmldoc.DocumentElement;

        XmlElement firstName = xmldoc.CreateElement("MyField1");
        firstName.InnerText = "John";
        root.AppendChild(firstName);

        XmlElement lastName = xmldoc.CreateElement("MyField2");
        lastName.InnerText = "Smith";
        root.AppendChild(lastName);

        XmlElement myField3 = xmldoc.CreateElement("MyField3");
        myField3.InnerText = "Field3 Value";
        root.AppendChild(myField3);

        XmlElement myField4 = xmldoc.CreateElement("MyField4");
        myField4.InnerText = "Field4 Value";
        root.AppendChild(myField4);

        xmldoc.Save(@"C:\simple.xml");

your example works partialy. What about prefixes? This is the point of my problem.

Check this:

XmlDocument xmldoc = new XmlDocument();

        XmlNode root = xmldoc.CreateElement("My", "Field", "Test");
        xmldoc.AppendChild(root); 
        
        XmlElement firstName = xmldoc.CreateElement("My", "Field1", "Test");
        firstName.InnerText = "John";        
        root.AppendChild(firstName);

        XmlElement lastName = xmldoc.CreateElement("My", "Field2", "Test");
        lastName.InnerText = "Smith";
        root.AppendChild(lastName);

        XmlElement myField3 = xmldoc.CreateElement("My", "Field3", "Test");
        myField3.InnerText = "Field3 Value";
        root.AppendChild(myField3);

        XmlElement myField4 = xmldoc.CreateElement("My", "Field4", "Test");
        myField4.InnerText = "Field4 Value";
        root.AppendChild(myField4);

        xmldoc.Save(@"C:\simple.xml");

much closer, but not exactly what I expected. But I think that I will use xpath to modify existing xml document instead creating new one.

regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.