A simple XML DOM interface for use with the Silverlight 1.1 Alpha

Posted 1 May 2007  

[RAW]

The Silverlight 1.1 Alpha release that everyone got their hands on Monday is full of a surprising amount of functionality considering that quick, tiny download.  I'd encourage everyone that hasn't picked up the Silverlight developer reference poster (which gives an overview of the namespaces and functionality you can take advantage of) to pick one up at MIX, or download a PNG of the poster from here.

In my spare time the past week I've been building a managed client app powered by Silverlight, targeting a few web services.  At this point I'm interfacing with Flickr's REST API, which returns results in a very simple XML payload which I need to parse; ideally interacting with it through a simple DOM interface instead of having to write a bunch of XmlReader code.  To keep the size of the framework down, the Core XML library doesn't include an XmlDocument interface.

I've implemented a quick wrapper over XmlReader to parse an XML string and provide simple DOM-style access to at least the elements by tagname.  This likely isn't very performant, so parsing a large document may affect the UI thread.  But its small, quick and works.

Feel free to check out the two classes, SimpleXmlDocument and SimpleXmlElement.  You can use a C# using alias in your application to use more familiar XML framework names if you like.

Start off by creating a new SimpleXmlDocument.  You'll then have access to the root node, and a set of element access methods including GetElementsByTagName, GetElementByTagName, and GetTextOfSingleTagName.

SimpleXmlDocument doc = new SimpleXmlDocument("<test><node>Some text</node></test>");
HtmlPage.Document.SetProperty("title", doc.Root.GetTextOfSingleTagName("node"));

CS files: SimpleXmlDocument.cs, SimpleXmlElement.cs
Include in your project and build for Silverlight with Whidbey or Orcas since its just simple C# 2.0 code.

Hope this helps!  Let me know if you build any cool apps or have suggestions, questions, or thoughts.

Disclaimer: This is just some prototype code I wrote to help with the application I was building.  I'm not a member of the XML team and this code isn't much of anything at all.  It isn't actually the DOM, probably can't handle a lot of things, but it works enough as it stands today for those Flickr responses.

[/RAW]

Jeff Wilcox is a Principal Software Engineer at Microsoft in the Open Source Programs Office, helping Microsoft scale to 10,000+ engineers using, contributing to and releasing open source.

comments powered by Disqus