开发者

C# XMLReader not parsing correctly

I don't want to use XMLDocument because i've written the XML Writing code using XMLWriter. So there should be no reason why to switch.

<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>开发者_如何学Go
    <Item>
      <Slot>27</Slot>
      <Id>1049</Id>
      <Amount>12</Amount>
    </Item>
  </InventoryItems>
  <BankItems />
</Player>

I'm trying to parse that out there. Here is what I got so far. Seems to break everywhere I had it working a bit with <Ignore>'s but thats when I used ReadToFollowing instead of ReadToNextSibling, it would work until ReadToFollowing hit a empty line.. and it would just hit up to 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);
            }
        }
    }   


So there should be no reason why to switch.

There's a very good reason to switch to a DOM-style representation unless your documents are too large to reasonably fit in memory: it's much easier to work with that representation.

XmlReader is frankly a pain to use. It's not clear exactly what's wrong (you say it "seems to break everywhere" but not exactly what's happening) but I would strongly advise you to move to a simpler model. Your code will be considerably simpler afterwards. If you can possibly use LINQ to XML instead of the pre-3.5 API, that will make your life even better.

If you absolutely insist on using XmlReader, I suggest you update your post with a simpler piece of XML and code which demonstrates the problem. I'd also suggest that you restructure your code to test for the node type being an element once, and then refactor the "handling an element" part into a separate method... where you may well want to switch on the element name and handle each kind of element in a separate method. Smaller methods are generally easier to understand, test and debug.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜