[英]WP7 - Parsing XML data
I always worked with easy xml structures and simple xaml bindings. 我总是使用简单的xml结构和简单的xaml绑定。 Now I am a bit confused while trying some complex stuff. 现在我在尝试一些复杂的东西时有点困惑。 I was reading this WP7 How to parse the XML? 我正在读这篇WP7如何解析XML? question and its answers but I couldn't understand the displaying the data part. 问题及其答案,但我无法理解显示数据部分。
I have similar XML Data like this: 我有类似这样的XML数据:
<?xml version="1.0"?>
<top>
<value name="Finals">
<country home="sweden" away="italy" venue="aaa"/>
</value>
<value name="Semi-finals">
<country home="Germany" away="sweden" venue="ccc"/>
<country home="france" away="italy" venue="ddd"/>
</value>
</top>
And the result I want to see is: 我想看到的结果是:
Finals
- Sweden - Italy in AAA
Semi-finals
- Germany - France in ccc
- France - Sweden in ddd
Is there a way to do this with Xaml binding stuff. 有没有办法用Xaml绑定的东西来做到这一点。 If you have any WP7 tutorial links about this I would be grateful. 如果您有关于此的任何WP7教程链接,我将不胜感激。
WPF has an XML binding API, however Silverlight for WP7 does not. WPF有一个XML绑定API,但是Silverlight for WP7没有。 I would use Linq to XML to create the string you are after. 我会使用Linq to XML来创建你追求的字符串。
Something like this should work ... 像这样的东西应该工作......
NL = System.Environment.NewLine;
doc = XDocument.Parse(xml);
StringBuilder output = new StringBuilder();
var rounds = doc.Descendants("value");
foreach(XElement round in rounds)
{
builder.Append(round.Attribute("value").Value + NL);
foreach(XElement country in round.Elements())
{
builder.Append(country.Attribute("home").Value);
builder.Append(" - ");
builder.Append(country.Attribute("away").Value);
builder.Append(" in ");
builder.Append(country.Attribute("venue").Value);
builder.Append(NL);
}
}
See the MSDN documentation for Linq to XML for more details. 有关更多详细信息,请参阅Linq to XML的MSDN文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.