Solved

C# + XML writing

Posted on 2006-10-22
2
227 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
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now