我在业余时间一直在制作一个愚蠢的小卡片游戏,并决定使用XML来存储卡片信息,因为我最近完成了一个XML课程。
问题是,课堂上使用的方法似乎不再起作用,也没有我在这里看到的替代方案。他们都没有抛出异常,所有人都可以看到文件,但拒绝从中提取任何内容。
XDocument,XmlDocument和XPathDocument看到文件就好了,所以问题不在那里。但是,XPathNavigator.Select()和XDocument.XPathSelectElements()根本不返回任何内容。
我目前的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>
尝试#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();
尝试#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();
尝试#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();
我甚至轮换了一些XPath样式,以防万一 - 但他们应该都达到同样的目的。
答案 0 :(得分:1)
好的,我使用了@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();
这将为我提供文件中所需的所有信息。