ProgressZero
ProgressZero

Reputation: 11

None of the XML parsing methods work for me

I've been working on a dumb little card game in my spare time, and decided to use XML for storing the card information, since I recently finished an XML class.

Problem is, the methods used in class suddenly don't seem to work anymore, nor the alternatives I've seen around here. None of them throw exceptions, and all of them can see the file, but refuse to extract anything from them.

XDocument, XmlDocument and XPathDocument see the file just fine, so the problem is not there. However, XPathNavigator.Select() and XDocument.XPathSelectElements() return nothing at all.

My current Cards.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Cards xmlns="http://tempuri.org/CardSchema.xsd">
  <Card>
    <Name>Emperor Heavy Tank</Name>
    <Type>Attack</Type>
    <Colour>Red</Colour>
    <Distinctions>
      <Distinction>Weapon</Distinction>
      <Distinction>Machine</Distinction>
    </Distinctions>
    <Health>25</Health>
    <Power>12</Power>
    <Ability>
      <Name>Repair Crew</Name>
      <Description>Heal for 4 HP, and apply 2 armour on the card if it is lacking. Increase turn wait for both skills by 1.</Description>
      <PowerUse>1</PowerUse>
      <TurnWait>1</TurnWait>
    </Ability>
    <Ability>
      <Name>High Caliber</Name>
      <Description>Deals 6 dmg to target, +2 if target is a Machine. If any adjacent cards are of the same colour, they recieve 3 spill damage, 1 otherwise. 4 turn wait.</Description>
      <PowerUse>5</PowerUse>
      <TurnWait>4</TurnWait>
    </Ability>
  </Card>
  <Card>
    <Name>Flamethrower</Name>
    <Type>Attack</Type>
    <Colour>Red</Colour>
    <Distinctions>
      <Distinction>Machine</Distinction>
      <Distinction>Weapon</Distinction>
    </Distinctions>
    <Health>13</Health>
    <Power>20</Power>
    <Ability>
      <Name>Roar of the Wheels</Name>
      <Description>All player ATK cards (besides Flamethrower) recieve +1 POW if the enemy doesn't have a red card in play. 1 turn wait.</Description>
      <PowerUse>2</PowerUse>
      <TurnWait>1</TurnWait>
    </Ability>
    <Ability>
      <Name>Flame Artillery</Name>
      <Description>Deals 7 dmg to non-red target. If target is red, deals 3 dmg, and does 2 dmg to both adjacent cards. 3 turn wait.</Description>
      <PowerUse>5</PowerUse>
      <TurnWait>3</TurnWait>
    </Ability>
  </Card>
</Cards>

Attempt #1:

XmlDocument Document = new XmlDocument();
Document.Load("Cards.xml");

var Navigator = Document.CreateNavigator();
var Expression = Navigator.Compile("/Cards/Card");
var Iterator = Navigator.Select(Expression);

while (Iterator.MoveNext()) // nothing to iterate through, so it just skips to the next statement immediately
{
    var HelpNavigator = Iterator.Current.Clone();
    Console.WriteLine(HelpNavigator.Value);
}

Console.ReadKey();

Attempt #2:

XPathDocument Document = new XPathDocument("Cards.xml");
var Navigator = Document.CreateNavigator();
var Expression = XPathExpression.Compile("//Card/*");
var Iterator = Navigator.Select(Expression);

do
{
    var InnerIterator = Iterator.Current.SelectChildren(XPathNodeType.Text);
    do
    {
        Console.WriteLine(InnerIterator.Current.Value);
    } while (InnerIterator.MoveNext());
} while (Iterator.MoveNext());

Console.ReadKey();

Attempt #3:

XDocument Document = XDocument.Load("Cards.xml");
var Elements = Document.XPathSelectElements("//Card/child::*");
foreach (var item in Elements) // and again, nothing
{
    Console.WriteLine("{0}: {1}", item.Name.ToString(), item.Value);
}

Console.ReadKey();

I even rotated through a few XPath styles, just in case - but they should all achieve the same thing.

Upvotes: 0

Views: 52

Answers (1)

ProgressZero
ProgressZero

Reputation: 11

Okay, I've used the method provided by @Corak:

XDocument Document = XDocument.Load("Cards.xml");
var xns = XNamespace.Get("http://tempuri.org/CardSchema.xsd");
var Elements = Document.Root.Elements(xns + "Card");
foreach (var Card in Elements)
{
    // Process card information here

    foreach (var item in Card.Elements())
    {
        Console.WriteLine("{0}: {1}", item.Name.ToString(), item.Value);
    }
}

Console.ReadKey();

This will provide me with all the info I need from the file.

Upvotes: 1

Related Questions