C#XMLReader无法正确解析

SSp*_*oke 0 c# xml xmlreader visual-studio

我不想使用XMLDocument,因为我使用XMLWriter编写了XML编写代码.因此,没有理由切换.

<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>
Run Code Online (Sandbox Code Playgroud)

我正在尝试解析那里.这是我到目前为止所得到的.似乎在任何地方打破了我有点工作<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);
            }
        }
    }   
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 7

因此,没有理由切换.

除非您的文档太大而不能合理地适合内存,否则切换到DOM样式表示是一个非常好的理由:使用该表示更容易.

XmlReader坦率地说,使用起来很痛苦.目前尚不清楚到底出了什么问题(你说它"似乎到处都是",但不完全是正在发生的事情)但我强烈建议你转向一个更简单的模型.之后您的代码将变得相当简单.如果你能可能使用LINQ到XML而不是预先3.5 API,这将让你的生活更加美好.

如果您绝对坚持使用XmlReader,我建议您使用更简单的XML和代码更新您的帖子,以证明问题.我还建议您重构代码以测试作为元素的节点类型一次,然后将"处理元素"部分重构为单独的方法...您可能希望切换元素名称和在一个单独的方法中处理每种元素.较小的方法通常更容易理解,测试和调试.