2011-06-28 47 views
0

我不想使用XMLDocument,因为我使用XMLWriter编写了XML编写代码。所以应该没有理由改变。C#XMLReader不能正确解析

<Player> 
    <Friends /> 
    <Ignores> 
    <Ignore>117779</Ignore> 
    <Ignore>44237636758361374</Ignore> 
    <Ignore>564534831</Ignore> 
    </Ignores> 
    <InventoryItems> 
    <Item> 
     <Slot>0</Slot> 
     <Id>995</Id> 
     <Amount>39493</Amount> 
    </Item> 
    <Item> 
     <Slot>27</Slot> 
     <Id>1049</Id> 
     <Amount>12</Amount> 
    </Item> 
    </InventoryItems> 
    <BankItems /> 
</Player> 

我试图解析那里。这是我到目前为止。似乎到处打破我有它与<Ignore>'s工作了一点点,但那是当我使用ReadToFollowing而不是ReadToNextSibling,它会工作,直到ReadToFollowing击中一个空行..它只会打到EOF。

XmlTextReader reader = new XmlTextReader(misc.getServerPath() + "\\accounts\\" + username + ".xml"); 
while (reader.Read()) 
{ 
    if (reader.NodeType == XmlNodeType.Element && reader.Name == "Friends") { 
     if (!reader.IsEmptyElement) //got any friends 
     { 
      while (reader.ReadToFollowing("Friend")) 
       //do_stuff_with_that_data(reader.ReadElementContentAsLong()); 
     } 
    } else if (reader.NodeType == XmlNodeType.Element && reader.Name == "Ignores") { 
     if (!reader.IsEmptyElement) //got any ignores 
     { 
      reader.ReadToFollowing("Ignore"); 
      while (reader.ReadToNextSibling("Ignore")) 
      { 
       //do_stuff_with_that_data(reader.ReadElementContentAsLong()); 
      } 
     } 
    } else if (reader.NodeType == XmlNodeType.Element && reader.Name == "InventoryItems") { 
     if (!reader.IsEmptyElement) //got items 
     { 
      int slot, id, amount; 
      while (reader.ReadToNextSibling("Item")) 
      { 
       reader.ReadToFollowing("Slot"); 
       slot = reader.ReadElementContentAsInt(); 
       reader.ReadToFollowing("Id"); 
       id = reader.ReadElementContentAsInt(); 
       reader.ReadToFollowing("Amount"); 
       amount = reader.ReadElementContentAsInt(); 
       //do_stuff_with_that_data(slot, id, amount); 
      } 
     } 
    } else if (reader.NodeType == XmlNodeType.Element && reader.Name == "BankItems") { 
     if (!reader.IsEmptyElement) //got bank items 
     { 
      int slot, id, amount; 
      while (reader.ReadToNextSibling("Item")) 
      { 
       reader.ReadToFollowing("Slot"); 
       slot = reader.ReadElementContentAsInt(); 
       reader.ReadToFollowing("Id"); 
       id = reader.ReadElementContentAsInt(); 
       reader.ReadToFollowing("Amount"); 
       amount = reader.ReadElementContentAsInt(); 
       //do_stuff_with_that_data(slot, id, amount); 
      } 
     } 
    } 

回答

7

所以应该没有理由进行切换。

有一个很好的理由来切换到DOM风格的代表,除非你的文件过大而合理地适合在内存:它更容易与表示工作。

XmlReader坦率地说是一种使用的痛苦。目前尚不清楚究竟发生了什么问题(你说它“似乎无处不在”,但不完全是这样),但我会强烈建议你转向一个更简单的模型。之后你的代码将变得相当简单。如果你可以可能使用LINQ to XML而不是3.5之前的API,那会让你的生活更加美好。

如果你绝对坚持关于使用XmlReader,我建议你用更简单的XML和代码来演示这个问题。我还建议你重构你的代码,以测试节点类型一次,然后将“处理元素”部分重构为一个单独的方法...在那里你可能想要打开元素在一个单独的方法中命名和处理每种元素。较小的方法通常更易于理解,测试和调试。

+1

如果您需要最高性能或者文档太大而无法放入RAM,XmlReader是非常棒的。对于我推荐使用LINQ to XML的其他东西 - 我已经能够用更简洁的<50行LINQ to XML查询替换> 200行XmlReader解析器。这是非常值得的! –