?
Solved

C# + XML writing

Posted on 2006-10-22
2
Medium Priority
?
255 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 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

801 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