C# + XML writing

RHopkinson used Ask the Experts™
I'm trying a little self study and i'm having a problem with the following. Any code help or if you know a resource that can explain how to properly write the code would be amazing.

Here is the output i'm looking for in my XML file:

<?xml version="1.0" ?>

Here is my code:

public void saveXML()
                  XmlDocument xmldoc;
                  XmlNode xmlnode;
                  XmlElement xmlelem;
                  XmlElement xmlelem2;
                  XmlText xmltext;

                  xmldoc=new XmlDocument();
                  //let's add the XML declaration section
                  //let's add the root element

                  for(int i=0; i < 3;i++)
                        //add a pair
                        xmlelem=xmldoc.CreateElement("","Pair","");         // <<--Recieve error here
                        xmldoc.AppendChild(xmlelem);                             //

                        //let's add another element (child of the Pair)
                        xmlelem = xmldoc.CreateElement("","Name","");
                        xmltext =xmldoc.CreateTextNode( "somevalue");

                        xmlelem2 = xmldoc.CreateElement("","Value","");
                        xmltext =xmldoc.CreateTextNode( "somevalue" );
                  //save the XML document in a file: C:\namevalue.xml

Thanks for your time!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Hi, when working with the XmlDocument class I find it easier to have seperate element objects for each of your XML elements (if theres a small amount of elements)... it allows you to construct the XML document easily as you have the references to the elements and all you have to do is to append to them etc, i've modified your code slightly and commented it:

public void saveXML()
      XmlDocument xmlDoc;
      XmlNode decNode;
      XmlElement rootElement, pairElement, nameElement, valueElement;

      xmlDoc = new XmlDocument();
      //Create and add the XML declaration
      decNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, null, null);
      //Create and add the root element Document
      rootElement = xmlDoc.CreateElement("Document");

      for(int i = 0; i < 3; i++)
            //Create a new Pair element and add it to the Root element
            pairElement = xmlDoc.CreateElement("Pair");

            //Create a new Name element, set its value and add it to Pair element
            nameElement = xmlDoc.CreateElement("Name");
            nameElement.InnerText = "Some name";

            //Create a new Value element, set its value and add it to Pair element
            valueElement = xmlDoc.CreateElement("Value");
            valueElement.InnerText = "Some value";
      //Save the XML document


Thanks for the quick reply and excellent code! very easy to understand!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial