C#XMLReader解析不正确

本文关键字:不正确 C#XMLReader | 更新日期: 2023-09-27 18:00:45

我不想使用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>

我试着分析一下。这是我到目前为止得到的。似乎到处都坏了,我用<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);
            }
        }
    }   

C#XMLReader解析不正确

因此,不应该有任何理由转换。

除非文档太大而无法合理地放入内存,否则有一个很好的理由切换到DOM样式的表示:使用该表示要容易得多

坦率地说,XmlReader使用起来很痛苦。目前还不清楚到底出了什么问题(你说它"似乎到处都坏了",但不清楚到底发生了什么),但我强烈建议你转向更简单的模型。之后,您的代码会简单得多。如果您可以可能使用LINQ to XML而不是3.5之前的API,这将使您的生活更加美好。

如果绝对坚持使用XmlReader,我建议您用一段更简单的XML和代码来更新您的文章,以演示这个问题。我还建议您重构代码,以测试节点类型是否为元素一次,然后将"处理元素"部分重构为一个单独的方法。。。其中您可能希望打开元素名称,并在单独的方法中处理每种元素。较小的方法通常更容易理解、测试和调试。