C # xml operations
There is a class in system. XML that can operate XML
(There are very few records of these classes on the Internet. It may be because there are many classes. msdn Introduction)
Create a New XML
Xmldocument xmldoc = New Xmldocument ();
// Add the XML declaration section to the header of the XML document
Xmlnode = xmldoc. createnode (xmlnodetype. xmldeclaration, "" , "" );
Xmldoc. appendchild (xmlnode );
// Create Root Node
Xmlelement root = xmldoc. createelement ( " Root " );
Root. innertext = " I am a root node " ;
Xmldoc. appendchild (Root );
Xmldoc. Save ( @" D: \ My Documents ents \ Visual Studio 2010 \ projects \ webapplication2 \ webapplication2 \ test. xml " );
The generated XML is like this.
<?XML version = "1.0"?>
<Root>I am a root node</Root>
Another method for creating XML
Xmldocument xmldoc =NewXmldocument ();
Xmldoc. loadxml (
"<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?>"+
"<Root> I am the root node </root>"
);
Xmldoc. Save (@"D: \ My Documents ents \ Visual Studio 2010 \ projects \ webapplication2 \ webapplication2 \ test. xml");
Read XML
XML is as follows
<? XML version = "1.0" encoding = "UTF-8" ?>
< Root ID = "Root" >
< List Num = "0" >
< Name Type = "String" > Minglecun </ Name >
< Age Type = "Number" > 55 </ Age >
</ List >
< List Num = "1" >
< Name Type = "String" > Huluwa </ Name >
< Age Type = "Number" > 1 </ Age >
</ List >
</ Root >
Xmldocument xmldoc = New Xmldocument ();
Xmldoc. Load ( @" D: \ My Documents ents \ Visual Studio 2010 \ projects \ webapplication2 \ webapplication2 \ test. xml " );
// Read a node
Xmlnode = xmldoc. selectsinglenode ( " // Root " );
Console. writeline (xmlnode. innerxml );
Console. writeline ( " ------------------------------------------------ " );
Xmlnode xmlnode1 = xmldoc. selectsinglenode ( " // Root // list " );
Console. writeline (xmlnode1.innerxml );
Console. writeline ( " ------------------------------------------------ " );
//
Xmlnodelist xn0 = xmldoc. selectsinglenode ( " // Root " ). Childnodes;
Foreach (Xmlnode Node In Xn0)
{
Console. writeline ( " ========== " + Node. Name );
}