by , Blog, No Comments

Aug 22

Dataset .ReadXml() does not import all rows

Capture_AppliedFilters

by JR Data, Blog, Comments Off

Aug 22

Just encountered a strange problem when working with a web project. I had an jQuery data object that I converted to XML structure using the library json2xml by Stefan Goessner. Later down the chain i had a WebMethod (aspx) to receive an ajax call from the HTML page done by jQuery, to save down the object data (now transferred to XML). In the WebMethod-function I use a standard non-typed dataset to read in the XML, as:

public static string SearchInformationContent(string xmlAppliedFilters)
{
dsAppliedFilters.ReadXml(new System.Xml.XmlTextReader(xmlAppliedFilters, System.Xml.XmlNodeType.Element, null));
}

xmlAppliedFilters contains the xml converted by the json2xml library. The XML looks like:

<AppliedFilters><InformationCategoryID>3</InformationCategoryID><Description>Fuel quality, Ppm sulfur limit for diesel fuel</Description><AppliedFilter>40</AppliedFilter><IsNumeric>True</IsNumeric></AppliedFilters><AppliedFilters><InformationCategoryID>64</InformationCategoryID><Description>Max. total weight (ton), Articulated vehicle 5 axles and more</Description><AppliedFilter>38</AppliedFilter><IsNumeric>False</IsNumeric></AppliedFilters>

The above xml is not well formed xml, but we overlook that at the moment. If you do ds.ReadXml() on the above XML structure, only the first row will get imported into the DataSet. If you step to the .ReadXml() again and re-run it, your DataSet will contain 3 rows. My conclusion is that the .ReadXML() function does not import the second row the first time, as the XML lacks a root element. The function of the DataSet to read in the XML does not at that point know how to handle the XML data, as it has no reference on how the XML structure is supposed to look like. If a root element exists, on the other hand, I does know how the strucure looks like, and then willingly imports all rows.

Applying a simple root element around the XML data, solved the problem:

dsAppliedFilters.ReadXml(new System.Xml.XmlTextReader("<Root>" + AppliedFilters + "</Root>", System.Xml.XmlNodeType.Element, null));

Obviously the right thing in the first place would be to supply a valid XML structure to the .ReadXML() function, – but still it does produce a strange result, as it does not complain about the XML source :)

    Comments are closed.