2

Ok, so I have this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Item>
    <Name>Iron Repeater</Name>
    <AutoReuse>true</AutoReuse>
    <UseAnimation>19</UseAnimation>
    <UseTime>19</UseTime>
    <Width>50</Width>
    <Height>18</Height>
    <Shoot>1</Shoot>
    <UseAmmo>1</UseAmmo>
    <UseSound>5</UseSound>
    <Damage>39</Damage>
    <ShootSpeed>11</ShootSpeed>
    <NoMelee>true</NoMelee>
    <Value>200000</Value>
    <Ranged>true</Ranged>
    <Rarity>4</Rarity>
    <Knockback>2.5</Knockback>

    <CraftStack>1</CraftStack>

    <CraftItem1>Wood</CraftItem1>
    <CraftValue1>1</CraftValue1>

    <CraftTile1>18</CraftTile1>

    <FinishCrafting/>

</Item>

And it's being read similar to this:

foreach (string s in API.itemFiles)
{
    using (XmlReader reader = XmlReader.Create(s))
    {
        string aTile;
        string aStack;
        string item;
        string iName;
        int tile;
        int stack;
        int iStack;
        reader.MoveToContent();
        while (reader.Read())
        {
            if (reader.NodeType == XmlNodeType.Element)
            {
                if (reader.IsStartElement())
                {
                    switch (reader.Name)
                    {
                        //Le cases here
                    }
                }
            }
        }
    }
}

API.itemFiles is:

public static string[] itemFiles = Directory.GetFiles(itemSave, "*.xml");

Whenever I attempt to read an XML file that way, it doesn't seem to parse the element content (I do readElementContentAsXX();) into variables or whatever, but it does seem to find the elements alright.

Is there anything I'm doing wrong? Anything I can improve on? If there are any other methods for reading XML (It'll be planning to have tons of XML files; I need it to be efficient) please say!

Thanks!

2 Answers 2

2

I can't find anything explicitly wrong with your solution. Switching on reader.Name worked when i tested it.

I'd recommend using some more more conventional XML format. Instead of numbered elements, you move those into sub-elements:

<Item>
    ...
    <Stacks>
        <Stack>
            <Item>Wood</Item>
            <Value>1</Value>
            <Tile>18</Tile>
        </Stack>
    </Stacks>
</Item>

You could then use XML object serialization to parse the files. It would be less error prone.

public class Item
{
    public string Name;
    public bool AutoReuse;
    public int UseAnimation;
    public int UseTime;
    public int Width;
    public int Height;
    public int Shoot;
    public int UseAmmo;
    public int UseSound;
    public int Damage;
    public int ShootSpeed;
    public bool NoMelee;
    public int Value;
    public bool Ranged;
    public int Rarity;
    public decimal Knockback;
    public List<Stack> Stacks;
}

public class Stack
{
    public string Item;
    public int Value;
    public int Tile;
}
XmlSerializer x = new XmlSerializer(typeof(Item));
var item = (Item) x.Deserialize(steam);
Sign up to request clarification or add additional context in comments.

1 Comment

Fully agree - good answer. I would suggest using properties, though.
0

"tons of XML files" isn't a problem; it only becomes an issue when you have a huge megalith of a single XML file. Since you don't have that, use a DOM. Either XmlDocument or XDocument will handle this trivially, and are much easier to usethan XmlReader. If you are mapping to classes (or could do), XmlSerializer would be a good option.

XmlReader is notoriously hard to use robustly without extensive knowledge and/or debugging (conversely, XmlWriter is a breeze).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.