RPGs, XML documents, stuff, things

Going from my happy little web-based world (with forms and spans and whatnot, with C# on the backend and JavaScript handling the stuff in between) to the XAML/Silverlight world has been like switching classes in an RPG. Those of you familiar with role-playing games of any genre will know what I’m talking about. You get really used to playing a rogue or a warrior, and then all of a sudden you decide to try out a warlock or mage. The way you play is completely different from class to class.

So I’m not only dealing with XAML newbtasticness, but I’m also struggling due to the fact that Silverlight 2.0 just doesn’t yet have the same documentation/tutorialization/discussion board saturation level as more established (I don’t dare say “legacy” hehehe) technologies. It’s almost got a hobbyist feel to it, and I kinda like that.

Anyway, I’ve been avoiding the ‘light for the past week due to illness (pinkeye and strep simultaneously!) and becuase I couldn’t figure out how to do something that would be dead simple for me in regular C# winforms/asp.net…how to read an XML file and use its data.

After some rigorous searching I finally figured out the following:
1. How to stash an XML document into your Silverlight app the right way: Create the document in your project, then right-click on it, go to Properties and set the Build Action to “Embedded Resource”. Now your document will be neatly tucked away inside your code file when the user downloads it.

2. How to read said XML document: First off, you’ll need to add the System.Xml.Linq namespace to your References in your project. Then, make sure you add your “using System.Xml.Linq;” statement (or VB equivalent) in the file you want to do XML magic with. THEN…do this:

XmlReader rdr = XmlReader.Create(this.GetType().Assembly.GetManifestResourceStream(“YourApplication.YourFileName.xml”));

You’re creating an XmlReader that grabs the file that you tucked away inside the application. Of course, you might want to toss this into a using block so it cleans up better, but you get the idea.

THEN…create an XDocument (a fancy Linq XmlDocument) like this:

XDocument xdoc = XDocument.Load(rdr);

see how it loads the content of the reader? nifty.

THEN…use Linq to XML to grab the stuff you need out of the XML. I’m not gonna cover that on this post. ScottGu does that much better. Download his example app and study it.

The part that was driving me crazy with this was the whole XDocument thing. I read some articles that said XDocument wasn’t even supported in Silverlight! Maybe they meant v.1? Anyway, I finally saw ScottGu mention that you have to reference System.Xml.Linq, and there was much joy in my heart as I finally saw all the pieces fit together.

I realize that what would probably help alleviate some of my frustration would be to go through all the tutorials I come across step by step and build my knowledge before I start my own projects…but man, that bores the hell out of me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: