RHopkinson
asked on
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" ?>
<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( XmlNodeTyp e.XmlDecla ration,"", "");
xmldoc.AppendChild(xmlnode );
//let's add the root element
xmlelem=xmldoc.CreateEleme nt("","Doc ument","") ;
xmldoc.AppendChild(xmlelem );
for(int i=0; i < 3;i++)
{
//add a pair
xmlelem=xmldoc.CreateEleme nt("","Pai r",""); // <<--Recieve error here
xmldoc.AppendChild(xmlelem ); //
//let's add another element (child of the Pair)
xmlelem = xmldoc.CreateElement("","N ame","");
xmltext =xmldoc.CreateTextNode( "somevalue");
xmlelem.AppendChild(xmltex t);
xmldoc.ChildNodes.Item(1). AppendChil d(xmlelem) ;
xmlelem2 = xmldoc.CreateElement("","V alue","");
xmltext =xmldoc.CreateTextNode( "somevalue" );
xmlelem2.AppendChild(xmlte xt);
xmldoc.ChildNodes.Item(1). AppendChil d(xmlelem2 );
}
//save the XML document in a file: C:\namevalue.xml
xmldoc.Save("c:\\namevalue .xml");
}
Thanks for your time!
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(
xmldoc.AppendChild(xmlnode
//let's add the root element
xmlelem=xmldoc.CreateEleme
xmldoc.AppendChild(xmlelem
for(int i=0; i < 3;i++)
{
//add a pair
xmlelem=xmldoc.CreateEleme
xmldoc.AppendChild(xmlelem
//let's add another element (child of the Pair)
xmlelem = xmldoc.CreateElement("","N
xmltext =xmldoc.CreateTextNode( "somevalue");
xmlelem.AppendChild(xmltex
xmldoc.ChildNodes.Item(1).
xmlelem2 = xmldoc.CreateElement("","V
xmltext =xmldoc.CreateTextNode( "somevalue" );
xmlelem2.AppendChild(xmlte
xmldoc.ChildNodes.Item(1).
}
//save the XML document in a file: C:\namevalue.xml
xmldoc.Save("c:\\namevalue
}
Thanks for your time!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER