C # xml operations

Source: Internet
Author: User

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.