in

WPF Design and Development

IdentityMine Team Blogs

David Kelley

A day in the life of a humble software architect... doing C#, WPF, Silverlight, Legos, Fuzzy Logic AI and/or whatever is the latest and greatest or more importantly the coolest techo mumbo jumbo...

Cross Browser XML Parsing in Silverlight

One problem we have seen and dealt with allot lately is parsing XML client side.  My favorite solution I must admit is to just toss out XML and use JSON as it is so much more elegant.  In the case of the ET Minisite we did as of late this turned out to solve a lot of our problems.  But still what if I can't do JSON for whatever reason?  if you only care about IE or even firefox that seems to be much less of a problem.  But for a really cross browser experience I my solution is the XML document mini me.  Granted this is a bit of hack (like much of JavaScript and Silverlight)... :)  but it works well... IF your XML is pretty stable and IF there isn't to much XML weirdness like multipul nested levels and such but it works. 

So the XML Mini me is a simple XML parseing engine written in Javascript.  It only supports a limited subset of something like the real xml parser in .NET like XMLReader or even XMLDocument but it does so a few simple things including some simple xPath like behavior.  I hestiate to say a subset of xpath as the mini me is so extremely limited.  I also suspect that the silverlight team at some point will have a good XML parser built in to silverlight at some point but we will see.  there is of course Silverlight 1.1 but if you can't do the .NET part it doesn't do much goo.

In Silverlight, you have the downloader object so its really reasonable get XML easily no matter what.  So then using the XML mini me looks something like this:

var MyDom = new XMLDocument();

MyDom.LoadXML( SomeXML);

I tried to keep the syntax as famliar as possible.  So now we have our XMLDOM called 'MyDom' and then to get data out we can do this:

var ArrayOfVAlues = MyDom.SelectNodes("//items");

In this case the mini me will return all the text values of all the items nodes in the dom in the form of an array.  Underneath the covers the parser is just doing some 'string' manipulation but for small groups or data or simple RSS feeds or config files it seems to work well.  large XML parseing would scary me preformance wise as would writing a more full featured parser that is entirely written in JavaScript.

Some things to note on the class is that it has an XML property like the XML parser we used to use in classic ASP but otherwise it only has two methods currently as those are the ones I need to get it working for my particular project.  lets look at the XML it can deal with for starters, here is a sample:

 <xml>

     <collection type="foobar" count="4">

          <item>

               <url>http://www.identitymine.com</url>

          <item>

          <item>

               <url>http://www.identitymine.com</url>

          <item>

          <item>

               <url>http://www.identitymine.com</url>

          <item>

          <item>

               <url>http://www.identitymine.com</url>

          <item>

          <item>

               <url>

                    <![CDATA[http://www.identitymine.com]]>

               </url>

          <item>

     </collection>

</xml>

So in this sample using some simple xml our parse deals with it easily.  In this case when we make a call to SelectNodes("//url") we will get an array of all the urls values with all the <![CDATA stuff stripped off into an array.  simple, easy and fast but easly breakable if you muck up the xml to much.  So for the time being its a great stop gap and if JavaScript works then this works at it uses only intrinsic language features.

Comments

 

simply24 » Blog Archive » Cross Browser XML Parsing in Silverlight said:

Pingback from  simply24  &raquo; Blog Archive   &raquo; Cross Browser XML Parsing in Silverlight

October 29, 2007 10:12 PM

About david.kelley

David for the past 10 years has focused on distributed application design and emerging Microsoft technologies on the web. Having helped design and build some of the largest systems for companies like Microsoft, Onyx Software, Saltmine, Giordanous Group and more and of course our favorite company Identitymine, he has been on the leading edge of applying the latest tech to real world business problems. David’s technology breadth includes everything from SQL Server to Windows/WCF and Silverlight. David’s accomplishments also include developing new technologies such as self editing XML files and related XML technologies to fuzzy logic systems and advanced web user interface design.
© 2007 IdentityMine, Inc.
Powered by Community Server (Commercial Edition), by Telligent Systems