Tuesday, March 16, 2010

Parsing XML with XDocument

I needed to parse a XML document recently in Silverlight. Unfortunately, Silverlight does not have System.Xml.XmlDocument type so you need to use System.Xml.Linq.XDocument.

The following example works in Silverlight and with small change also in WPF.

# encoding: utf-8
import clr
clr.AddReferenceToFile('System.Xml.Linq.dll')
from System.Xml.Linq import XDocument, XNamespace

content = """<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss"
    xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <item>
            <title>This is the title</title>
            <media:description type="html"><p></p></media:description>
            <link>html/dsc00001.html</link>
            <media:thumbnail url="preview/dsc00001.jpg"/>
            <media:content url="web/dsc00001.jpg"/>
        </item>
        <item>
            <title></title>
            <media:description type="html"><p></p></media:description>
            <link>html/dsc00002.html</link>
            <media:thumbnail url="preview/dsc00002.jpg"/>
            <media:content url="web/dsc00002.jpg"/>
        </item>
    </channel>
</rss>"""

xDoc = XDocument().Parse(content)
namespace = XNamespace.Get("http://search.yahoo.com/mrss")
for item in xDoc.Element('rss').Element('channel').Elements('item'):
    print item.Element('title').Value
    print item.Element(namespace+'thumbnail').Attribute('url').Value
Here is the output:
This is the title
preview/dsc00001.jpg

preview/dsc00002.jpg

You have to have System.Xml.Linq.dll from Silverlight SDK next to your app.py.

The change for WPF:

clr.AddReference('System.Xml.Linq')

Also make sure you don't have Silverlight's System.Xml.Linq.dll next to your script.

No comments: