This is a simple demonstration on XML Parsing. It’s a primitive knowledge and I added a drop down effect to make it looks much more attractive.
The Silverlight version can only load the XML from the resources. It’s because I don’t have IIS right now and I couldn’t solve the cross domain security problem (Better don’t waste too much time on that first). Enjoy it.
Vote for this sample
Comparison
Flash implementation: 1 hour
Silverlight implementation: 1 hour 30 minutes
What’s the difference?
- XML Prasing: XML [AS3] vs XDocument [C#]
Source codes
Simple XML Parser [Flash 9, AS3] (26.9 KiB, 956 hits)
Simple XML Parser [Silverlight 2, C#] (94.6 KiB, 1,200 hits)
Flash
Silverlight
XML Prasing: XML [AS3] vs XDocument [C#]
XML is a very difficult topic in many languages, because there are dozen of methods to manipulate XML. If you want to learn something more, here is a good site: http://www.senocular.com/flash/tutorials/as3withflashcs3/?page=4. Anyway, let’s have a brief view how did I get all the titles from a RSS feed.
// AS3
// create a loader and load the XML
var loader : URLLoader = new URLLoader();
var request : URLRequest = new URLRequest(url);
loader.addEventListener(Event.COMPLETE, on_complete);
loader.load(request);
// after the request is completed
private function on_complete(e : Event):void{
var loader:URLLoader = e.target as URLLoader;
var xmlData : XML = new XML(loader.data);
var titleList : XMLList = xmlData.channel.item.title;
for(var i:int = 0; i < titleList.length(); i++){
var node :XML = titleList[i];
// get the title
var title:String = node.text();
}
}
You may use LINQ query in C# and the data can be collected easily (if you really know how to use it.)
// C#
// url is the xml data from the xap resources
XDocument xDocument = XDocument.Load(url);
var v = from g in xDocument.Descendants("item")
select new
{
title = g.Element("title").Value,
};
foreach (var item in v)
{
string title = item.title.Trim();
}