Solved

C# + XML writing

Posted on 2006-10-22
2
247 Views
Last Modified: 2010-04-16
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" ?>
<Document>
     <Pair>
          <Name>somevalue</Name>
          <Value>somevalue</Value>
     </Pair>
     <Pair>
          <Name>somevalue</Name>
          <Value>somevalue</Value>
     </Pair>
</Document>

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
                  xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
                  xmldoc.AppendChild(xmlnode);
                  
                  //let's add the root element
                  xmlelem=xmldoc.CreateElement("","Document","");
                  xmldoc.AppendChild(xmlelem);

                  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");
                        xmlelem.AppendChild(xmltext);
                        xmldoc.ChildNodes.Item(1).AppendChild(xmlelem);

                        xmlelem2 = xmldoc.CreateElement("","Value","");
                        xmltext =xmldoc.CreateTextNode( "somevalue" );
                        xmlelem2.AppendChild(xmltext);
                        xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);
                  }
                  //save the XML document in a file: C:\namevalue.xml
                  xmldoc.Save("c:\\namevalue.xml");
            }

Thanks for your time!
0
Comment
Question by:RHopkinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 125 total points
ID: 17785516
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);
      xmlDoc.AppendChild(decNode);
               
      //Create and add the root element Document
      rootElement = xmlDoc.CreateElement("Document");
      xmlDoc.AppendChild(rootElement);

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

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

            //Create a new Value element, set its value and add it to Pair element
            valueElement = xmlDoc.CreateElement("Value");
            valueElement.InnerText = "Some value";
            pairElement.AppendChild(valueElement);
      }
                  
      //Save the XML document
      xmlDoc.Save(@"c:\namevalue.xml");
}
0
 

Author Comment

by:RHopkinson
ID: 17785568
Thanks for the quick reply and excellent code! very easy to understand!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question