C# + XML writing

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!
Who is Participating?
Ravi SinghConnect With a Mentor Senior Software EngineerCommented:
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
RHopkinsonAuthor Commented:
Thanks for the quick reply and excellent code! very easy to understand!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.