Generate XML file

View Code
   
     
private void btnCreateXML_Click( object sender, EventArgs e)
{
XmlDocument doc
= new XmlDocument();
doc.LoadXml(
" <xml version=\"1.0\" encoding=\"utf-8\" /> " );

XmlElement newElem
= doc.CreateElement( " price " );
newElem.InnerText
= " 10.95 " ;
doc.DocumentElement.AppendChild(newElem);

XmlElement childElem1
= doc.CreateElement( " Item1 " );
doc.DocumentElement.AppendChild(childElem1);

XmlElement childElem2
= doc.CreateElement( " Item2 " );
doc.DocumentElement.AppendChild(childElem2);
doc.Save(
" test.xml " );

lblMessage.Text
= " Generate xml file sucessfully! " ;
}

private void button1_Click( object sender, EventArgs e)
{
XmlDocument doc
= new XmlDocument();
doc.LoadXml(
" <book> " +
" <title>Oberon's Legacy</title> " +
" <price>5.95</price> " +
" </book> " );

// Create a new element node.
XmlNode newElem = doc.CreateNode( " element " , " pages " , "" );
newElem.InnerText
= "" ;

Console.WriteLine(
" Add the new element to the document... " );
XmlElement root
= doc.DocumentElement;
root.AppendChild(newElem);

XmlNode newElem1
= doc.CreateNode( " element " , " pages " , "" );
newElem1.InnerText
= "" ;


newElem.AppendChild(newElem1);

Console.WriteLine(
" Display the modified XML document... " );
Console.WriteLine(doc.OuterXml);
}

你可能感兴趣的:(File)