C # How to read XML node content

Source: Internet
Author: User
Using system;
Using system. xml;
Using system. xml. XPath;
Using system. Data;

Class readxml
{
Public static void main ()
{
String sfile = "readxml. xml ";

// Method 1
Xmldocument Doc = new xmldocument ();
Doc. Load (sfile );
Xmlnode node = Doc. documentelement ["news"] ["content"];
Console. writeline (node. innertext );

// Method2
Node = Doc. selectsinglenode ("// content ");
Console. writeline (node. innertext );

// Similarly
Node = Doc. documentelement. selectsinglenode ("News/content ");
Console. writeline (node. innertext );

// Method 3
Dataset DS = new dataset ();
DS. readxml (sfile );
Console. writeline (Ds. Tables [0]. Rows [0] ["content"]. tostring ());

// Method 4
Xmltextreader reader = new xmltextreader (sfile );
While (reader. Read ())
{
If (reader. Name = "content ")
{
Console. writeline ("***" + reader. readstring ());
Break;
}
}

Reader. Close ();

// Method 5

Xpathdocument xpdoc = new xpathdocument (sfile );
Xpathnavigator xpnv = xpdoc. createnavigator ();
Xpnv. movetofirstchild ();
Xpnv. movetofirstchild ();
Xpnv. movetofirstchild ();
Xpnv. movetonext ();
Console. writeline ("pathnavigator:" + xpnv. value );
}
}

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.