>>>>XmlTextReader在读取一个xml文件的时候始终侍从xml文件最后一个节点开始读取的吗?????should start from the beginningshow your xml and complete C# code

解决方案 »

  1.   

    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
      

  2.   

    应该是 && xmlreader.HasAttributes 这句的问题吧?有可能节点都没有属性,试试把它去掉再运行一下看看。
      

  3.   

    同意webdiyer(陕北吴旗娃) ,我也觉得可能属性那句话有问题。
      

  4.   

    问题解决了!
    把原来的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)
      {
        ....
      }请高手指点!!!
      

  5.   

    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