This morning I was having a battle royale with a small xml file and fighting with the idea of "what's the best way". As usual, there are approx. 2 billion ways to do it, about 3 of them are the "right" way and, of course, there's 1 that's the best way. Mildly annoyed (xml files and I have a history, mutual love/hate relationship) I tossed the question out and Lori says to use xsd.exe to create the things I need. Ahhh yes, xsd.exe, how I remember thee.
the ole xsd has come along since last I used it with 2.0 and I was very happy to see this ...
| Option | Description |
|
/enableDataBinding
|
Implements the INotifyPropertyChanged interface on all generated types to enable data binding. The short form is '/edb'.
|
enable databinding with INotify! Sweeeeeeet. They also include a linq aspect of it as well. Nice. I don't need the linq part, but that INofity will be perfect. To create the xsd, I ran ...
xsd C:\xmldata\myXmlFile.xml /out:C:\xmldata\
and get my nice xsd file. From there, I want the classes with that awesome INotify (/edb) and I want to start on a particular element makes a cleaner class (/e:myXmlElement) and I want it in c# (/l:cs) ...
xsd C:\xmldata\myXmlFile.xsd /classes /e:myXmlElement /l:CS /edb /out:C:\xmldata
It's too bad the output class STILL comes with Arrays. I like lists myself, but can't complain, easy fix. Now lets make this do something useful...like this.
StreamReader reader = new StreamReader(Server.MapPath("MyXmlFile.xml"));
XmlSerializer serial = new XmlSerializer(typeof(MyXmlFile));
MyXmlFile xmlData = new MyXmlFile();
xmlData = serial.Deserialize(reader) as MyXmlFile;
this.RadGrid1.DataSource = xmlData;
this.RadGrid1.DataBind();
Solid.