2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by krokodajl
0

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");
0

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

0

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");
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.