. Net (C #): xmlreader, whitespace, And the movetocontent and readtofollowing Methods

Source: Internet
Author: User

Xmlreader reads whitespace and comments from XML files by default.

 

For example, the XML:

<Root>

<! --Root-->

<File>

<File. ATTR>Hidden</File. ATTR>

A.txt

</File>

</Root>

 

Xmlreader readingCode:

// Data. xml indicates the preceding XML file

Using(VaRXR= Xmlreader.Create ("Data. xml"))

{

While(XR.Read ())

Console.Writeline ("Nodetype: {0,-20} Name: {1 }", XR.Nodetype, XR.Name );

}

 

The output is as follows:

Nodetype: xmldeclaration name: XML

Nodetype: whitespace Name:

Nodetype: element name: Root

Nodetype: whitespace Name:

Nodetype: Comment Name:

Nodetype: whitespace Name:

Nodetype: element name: File

Nodetype: whitespace Name:

Nodetype: element name: file. ATTR

Nodetype: Text Name:

Nodetype: endelement name: file. ATTR

Nodetype: Text Name:

Nodetype: endelement name: File

Nodetype: whitespace Name:

Nodetype: endelement name: Root

 

As you can see, whitespace and comments are read in.

 

One solution is to set ignorewhitespace and ignorecomments to true (false by default) in xmlreadersettings, and then create xmlreader:

VaRXrs= New Xmlreadersettings();

Xrs.Ignorecomments= True;

Xrs.Ignorewhitespace= True;

Using(VaRXR= Xmlreader.Create ("Data. xml", Xrs ))

{}

 

You can also use the movetocontent method of xmlreader, you can refer to the comprehensive interpretation of msdn (http://msdn.microsoft.com/zh-cn/library/system.xml.xmlreader.movetocontent.aspx ):

Check whether the current node is a content (non-blank text, CDATA, element, endelement, entityreference, or endentity) node. If this node is not a content node, the reader jumps forward to the next content node or the end of the file. It skips nodes of the following types: processinginstruction, documenttype, comment, whitespace, or significantwhitespace.

 

Code:

// Data. xml indicates the preceding XML file

Using(VaRXR= Xmlreader.Create ("Data. xml"))

{

While(XR.Read ())

{

XR.Movetocontent ();

Console.Writeline ("Nodetype: {0,-20} Name: {1 }", XR.Nodetype, XR.Name );

}

}

 

Output:

Nodetype: element name: Root

Nodetype: element name: File

Nodetype: element name: file. ATTR

Nodetype: Text Name:

Nodetype: endelement name: file. ATTR

Nodetype: Text Name:

Nodetype: endelement name: File

Nodetype: endelement name: Root

 

Whitespace and comments are missing.

 

Be sure not to confuse the two other methods of xmlreader: movetoattribute and movetoelement, which move between XML elements and attributes.

 

In addition, many xmlreader read Operations call the movetocontent method internally, such as readelementstring, readstartelement, readendelement ......

 

 

The third method is the movetofollowing method, which will continue to read until the element with the specified name is found. In this way, we can directly find the desired node and omit those whitespaces or comments.

Code:

// Data. xml indicates the preceding XML file

Using(VaRXR= Xmlreader.Create ("Data. xml"))

{

// Read the file. ATTR element using readtofollowing

XR.Readtofollowing ("File. ATTR");

Console.Writeline (XR.Readelementstring ());

// Read the XML text in the element

Console.Writeline (XR.Readstring ().Trim ());

}

 

Output:

Hidden

A.txt

 

 

Note that the whitespace of the text nodes in the XML element is always retained.

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.