Dan Wahlin saves my sorry, ignorant butt once again
I've lost count of how many times Dan Wahlin, either directly through e-mail or indirectly through his writing, has helped me out with XML-based problems in my projects. I was able to tap Dan's wisdom yet again this morning as I sussed out a nagging problem with inferred schemas and XML's nature to assume all fields are strings in lieu of a more formal validation structure.
I was able to figure out a curious problem I had with overriding the default inferred XML schema applied to a DataSet, which was ruining what was intended to be numeric sorting. I took a look at Dan's timeless, seminal work "XML for ASP.NET Developers" and literally slapped myself up the head when I discovered found 2 key points:
- the XML Schema needs to be loaded first into a DataSet before the XML itself is read into the DataSet (not the other way around)
- you need to call DataSet.AcceptChanges() - D-UH!
I was previously trying unsuccessfully to apply conditional XS:INT and XS:DOUBLE typing to certain fields using the following construct:
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("results.xml"));
ds.ReadXmlSchema(Server.MapPath("results.xsd"));
ds.WriteXmlSchema(Server.MapPath("results-2.xsd");
...and here's the code that got it working:
DataSet ds = new DataSet();
ds.ReadXmlSchema(Server.MapPath("results.xsd"));
ds.ReadXml(Server.MapPath("results.xml"));
ds.AcceptChanges();
Then, running a test with WriteXmlSchema() shows the data typing has taken effect, and the page in which the code executes clearly shows that the data is properly sorted numerically.
Thanks Dan! I owe you some serious BBQ if you ever head out this way!