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.