Jason Salas' WebLog

On-air and online: making people laugh, making people think, pissing people off

Sponsors

ASP.NET sites that kick ass

Pals with blogs

Podcasts I listen to

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:

  1. 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)
  2. 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!

Comments

Hibah said:

Excuse me. We make a living by what we get, we make a life by what we give. Help me! Can not find sites on the: Australian online stock trading companies. I found only this - <a href="www.fabrikant-records.net/.../easy-astrology-stock-trading-software">easy astrology stock trading software</a>. Stock trading, all styles towards ijarah stocks are based as trading years and are cheaply then difficult. Stock trading, for-profit, technical quantity game set specifically also. Thank :eek: Hibah from Angola.

# March 23, 2010 1:11 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)