请帮我从我的XML文件中读取一个字符串,然后打印出来的一个消息
这是我的XML文件:请帮我从我的XML文件中读取一个字符串,然后打印出来的一个消息
<?xml version="1.0" encoding="utf-8" ?>
<Hero>
<Legion>
<Andromeda>
<HeroType>Agility</HeroType>
<Damage>39-53</Damage>
<Armor>3.1</Armor>
<MoveSpeed>295</MoveSpeed>
<AttackType>Ranged(400)</AttackType>
<AttackRate>.75</AttackRate>
<Strength>16</Strength>
<Agility>27</Agility>
<Intelligence>15</Intelligence>
<Icon>Images/Hero/Andromeda.gif</Icon>
</Andromeda>
<WitchSlayer>
<HeroType>Agility</HeroType>
<Damage>39-53</Damage>
<Armor>3.1</Armor>
<MoveSpeed>295</MoveSpeed>
<AttackType>Ranged(400)</AttackType>
<AttackRate>.75</AttackRate>
<Strength>16</Strength>
<Agility>27</Agility>
<Intelligence>15</Intelligence>
<Icon>Images/Hero/Andromeda.gif</Icon>
</WitchSlayer>
</Legion>
</Hero>
这里是我的方法,但它不能正常工作,所以我不知道是什么去做。
public string GetHeroIcon(string Name)
{
//Fix later. Load the XML file from resource and not from the physical location.
HeroInformation = new XPathDocument(@"C:\Users\Sergio\Documents\Visual Studio 2008\Projects\Erth v0.1[WPF]\Tome of Newerth v0.1[WPF]\InformationRepositories\HeroRepository\HeroInformation.xml");
Navigator = HeroInformation.CreateNavigator();
Navigator.MoveToRoot();
Navigator.MoveToChild("Witch","Legion");
string x = "";
do
{
x += Navigator.Value;
} while (Navigator.MoveToNext());
return x;
}
我需要别人帮助的是临危字符串参数“名称”,然后返回所有的XML元素的属性的方法。
在伪代码:
public void FindHero(string HeroName)
{
//Find the "HeroName" element in the XML file.
//For each tag inside of the HeroName parent element,
//add it to a single string and blast it out through a MessageBox.
}
我正在学习如何使用这个,请不要离开势利的言论一样,“我们不会为你做这个。”我在这里并没有要求开创性的东西,只是一个简单的用例,用于我的程序和我的学习。 :D我正在做WPF中的整个应用程序,我可以从字面上说我以前没有做过一件事,我只是在业余时间学习新事物。
谢谢你,所以你摇滚!
private static string GetHeroIcon(string name)
{
XDocument doc = XDocument.Load("C:/test.xml");
return doc.Descendants(name).Single().Element("Icon").Value;
}
XDocument使这更容易。 – 2009-12-20 00:57:27
首先,既然您已经标记了这个问题的WPF,那么您应该知道WPF对直接绑定到XML数据的支持非常好。然后可以将GUI中的图像直接映射到XML文件中的图标元素。请参阅此链接,例如:http://www.longhorncorner.com/UploadFile/cook451/DataBindingXAML12102005134820PM/DataBindingXAML.aspx(先打在谷歌“WPF绑定XML”)
从代码,您可以从XML文件中创建一个XPathDocument,然后get a Navigator终于在其上运行自定义XPath查询,如下所示:
// Get's the value of the <icon> tag for a hero
var node = myNavigator.SelectSingleNode("/Legion/Hero/" + nameOfHero + "/Icon");
var icon = node.Value;
// To get all the nodes for that hero, you could do
var nodeIter = myNavigator.Select("/Legion/Hero/" + nameOfHero)
var sb = new StringBuilder();
while (nodeIter.MoveNext())
{
sb.AppendLine(nodeIter.Current.Name + " = " + nodeIter.Current.Value);
}
MessageBox.Show(sb.ToString());
查看this kb article举例。
免责声明:我复制并粘贴了代码,并在此窗口中进行了一些重构。它可能不会在第一次运行时编译,但这可能意味着需要10分钟才能将其运送到需要的位置。
我强烈建议您使用XML反序列化。它是面向对象的,类型安全的,只是光滑的。
试试这个: 1)创建一系列类:一个用于英雄,军团,女巫杀手和仙女座。
这里是仙女座类的例子:
using System.Xml.Serialization;
[XmlRoot("Andromeda")]
public class Andromeda
{
[XmlElement("Damage")]
public String Damage
{
get;set;
}
[XmlElement("Armor")]
public double Armor
{
get;set;
}
}
英雄类应包含军团和军团的实例应包含其余模仿XML数据包的布局。
2)使用XmlSerializer反序列化数据:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Hero));
using (StringReader reader = new StringReader(xmlDataString))
{
Hero hero = (Hero) xmlSerializer.Deserialize(reader);
}
如果你设置它的权利,你会留下包含嵌套对象和所有数据的英雄实例。很酷,嗯?
我说过,因为他的评论与MSDN上的例子有关。你的评论之后是否有链接? – 2009-12-19 22:55:02
我也不确定你为什么以这种方式使用XML。它不应该是' ...'? –
2009-12-19 23:00:39