Solved

C# + XML writing

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

25 Experts available now in Live!

Get 1:1 Help Now