2.0 Framwork Enhancements on System.XML - And misc ramblings
For the past few weeks, I've been upskilling as the lower cased one calls it. I'm also to get back into the groove at the newsgroups and about the only thing I'm slacking on is support over at my beloved Devbuzz which I'll be getting back into this weekend. Stuff I've been working on mainly consists of learning System.Xml inside and out (and of course XPath and XSLT), Speech Server and miscellaneous tidbits of 2.0 stuff.
The fact that I'm an ADO.NET junkie is probably readily obvious. And recently, I've realized that as much as I love ADO.NET, I've been using it as a crutch. Don't get me wrong, this has nothing to do with flaws in ADO.NET, it has flaws to do with Bill. So why is it a crutch? Well, the quote is attributed to a lot of different people, but it says essentially “He who is good with a hammer sees the world as a nail.” This is particularly true in my case. I don't think there's a problem out there that I can't solve using a DataRelation, DataTable.Select, DataTable.Compute, GetChildRows and WriteXml, ReadXml. If it's XML and I need to manipulate it, I can read it into a dataset, count the tables, count the rows in each table and start DataTable.Select ing or DataView.RowFilter ing. The problem arises when I need to get it back into XML. And what I often do is just use WriteXml and work around it (or tell guy on the other end to work around it). This is LAME on my part so I decided to cure it.
Well, that's my story and I'm sticking with it. Probably the 'real' reason I decided to learn XML is because I wanted to outPimp el Maliko in ADO.NET and I can't get there without XML. Actually, that's not the real reason either. The real reason is because theres a newly minted MVP who's notorious for convoluted and inaccurate answers and I've been reading his posts. I typically look down my nose at him with disdain b/c I think he's a jerk and his answers are either incorrect or plagairized. Anyway, every friggin XML question that pops up, he uses the same method I did. People ask about XPathIterators and he goes into 'load it into a dataset'. I was never that bad but I was close. Anyway, I'm 1/3 of my way to my goal. Right now I own a few chunks of the System.Xml namespace, XPath in particular. XSLT is giving me a little trouble, not so much with the syntax but wiht knowing what the f*** I want to transform everything to.
If you are using DataSets and you need to give people Xml in a specific format, probably the best bet is to use Xslt. From what I can tell, it's either that, or iterate the DataTables and build an XML document as you iterate them. This is a royal pain in the a33 and of very little value. Code wise it's a nightmare and if you need to change the output, you need to recompile as opposed to just editing your Xslt.
String FailoverNamespace = "williamgryan.cuckoozbackup.com";
xmlPen.WriteStartDocument();
xmlPen.WriteStartElement("Cuckooz", PrimaryNamespace);
xmlPen.WriteStartElement("Cuckoo", PrimaryNamespace);
xmlPen.WriteEndElement();
xmlPen.WriteEndElement();
xmlPen.WriteEndDocument();
xmlPen.Close();
2.0 Way
Now you can just use the static Create method
XmlWriter xmlPen = XmlWriter.Create
(@"C:\Upskilling\XML\samplecuckoo.xml");
Ok, not much different, until you look at the friggin
overloads. Now you can use the
XmlWriterSettings class...
XmlWriterSettings docSettings = new XmlWriterSettings();
docSettings.Indent = {true/false};
docSettings.CloseOutput = {true/false};
docSettings.Encoding = 'Any value of the Encoding
enumeration ie UTF16
docSettings.ConformamceLevel =
ConformanceLevel.Fragment;
You can set most of the behavioral settings here and then
just pass it in the in
the constructor:
XmlWriter xmlPen = XmlWriter.Create
(@"C:\Upskilling\XML\samplecuckoo.xml", docSettings);
Ok, this is a lame example I know. I'm working on a class to read through the RSS feed using some of the new 2.0 Objects. Let me finish that up and I'll post it in a few hours.
The long and short of it is though - that XML is becoming a lot more data centric, particularly in the 2.0 framework and the days of getting by withouth really being able to navigate and manipulate it are coming to an end. At least they are for me.