Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP7 - Parsing XML data

I always worked with easy xml structures and simple xaml bindings. Now I am a bit confused while trying some complex stuff. I was reading this WP7 How to parse the XML? question and its answers but I couldn't understand the displaying the data part.

I have similar XML Data like this:

<?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. If you have any WP7 tutorial links about this I would be grateful.

like image 876
FrankCap Avatar asked Aug 02 '26 17:08

FrankCap


1 Answers

WPF has an XML binding API, however Silverlight for WP7 does not. I would use Linq to XML to create the string you are after.

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.

like image 183
ColinE Avatar answered Aug 04 '26 06:08

ColinE



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!