for example, for this xml:Testxml.xml<root> <child value="1"/> <child value="2"> <child2 value="3"/> <child2 value="4"> <child3 value="5"/> <child3 value="6"/> </child2> </child> <child value="7"> <child2 value="8"/> </child> </root>with this code: XmlTextReader xtr = new XmlTextReader("TestXml.xml"); while (xtr.Read()) { Console.Write("{0}, {1}", xtr.Name, xtr.NodeType); if(xtr.NodeType==XmlNodeType.Element && xtr.HasAttributes) { Console.Write(" is an element and has attributes"); } Console.WriteLine(); } xtr.Close();========= you will get =========root, Element , Whitespace child, Element is an element and has attributes , Whitespace child, Element is an element and has attributes , Whitespace child2, Element is an element and has attributes , Whitespace child2, Element is an element and has attributes , Whitespace child3, Element is an element and has attributes , Whitespace child3, Element is an element and has attributes , Whitespace child2, EndElement , Whitespace child, EndElement , Whitespace child, Element is an element and has attributes , Whitespace child2, Element is an element and has attributes , Whitespace child, EndElement , Whitespace root, EndElement
remember people having problems with Chinese pages unless they modify web.config to have GB2312 encoding? in ASP.NET, StreamReader works similarly, so you have to specify an encoding here too
<child value="1"/>
<child value="2">
<child2 value="3"/>
<child2 value="4">
<child3 value="5"/>
<child3 value="6"/>
</child2>
</child>
<child value="7">
<child2 value="8"/>
</child>
</root>with this code:
XmlTextReader xtr = new XmlTextReader("TestXml.xml");
while (xtr.Read())
{ Console.Write("{0}, {1}", xtr.Name, xtr.NodeType);
if(xtr.NodeType==XmlNodeType.Element && xtr.HasAttributes)
{
Console.Write(" is an element and has attributes");
} Console.WriteLine();
} xtr.Close();=========
you will get
=========root, Element
, Whitespace
child, Element is an element and has attributes
, Whitespace
child, Element is an element and has attributes
, Whitespace
child2, Element is an element and has attributes
, Whitespace
child2, Element is an element and has attributes
, Whitespace
child3, Element is an element and has attributes
, Whitespace
child3, Element is an element and has attributes
, Whitespace
child2, EndElement
, Whitespace
child, EndElement
, Whitespace
child, Element is an element and has attributes
, Whitespace
child2, Element is an element and has attributes
, Whitespace
child, EndElement
, Whitespace
root, EndElement
把原来的XmlTextReader 改为如下:
XmlTextReader xmlreader=new XmlTextReader(new StreamReader (Xmlpath,System.Text.Encoding.GetEncoding("GB2312")));增加了Xml编码!但是还是比较奇怪,我的Xml文档中自带了编码“GB2312”,为什么还要我
在读取的时候增加增加Xml编码呢???我的Xml文档如下:
<?xml version="1.0" encoding="GB2312"?>
<_ROOT_>
<ITEMS>
<ITEM FILENAME="文档管理功能设计" FILESIZE="1226" FILETYPE="TXT" CREATIME="2003-10-10 23:00:00" OGUID="0df1e4fe-d98f-4949-8e1e-8d074c985183" SUPER_OGUID=""/>
<ITEM FILENAME="文档管理功能设计" FILESIZE="1226" FILETYPE="TXT" CREATIME="2003-10-10 23:00:00" OGUID="38aae587-6f26-4d85-b5dc-3045e75fdf23" SUPER_OGUID=""/>
<ITEM FILENAME="文档功能设计" FILESIZE="1226" FILETYPE="TXT" CREATIME="2003-10-10 23:00:00" OGUID="38aae587-6f26-4d85-b5dc-3045e75fdf23" SUPER_OGUID=""/>
</ITEMS>
</_ROOT_>
代码如下:
XmlTextReader xmlreader=new XmlTextReader(new StreamReader(Xmlpath,System.Text.Encoding.GetEncoding("GB2312")));
xmlreader.WhitespaceHandling= WhitespaceHandling.Significant;
while(xmlreader.Read())
{
if(xmlreader.NodeType==XmlNodeType.Element && xmlreader.HasAttributes)
{
....
}请高手指点!!!