Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
210
How to read xml files into UltraGrid with one schema file
posted

Hi,

We are reading xml files in an UltraGrid using below code, with this code we can be able to read any xml file data into UltraGrid. But the problem with the Data Type of a column. While reading XML it's taking column data type as String. Due to that we are facing issue with "Sorting" of Integer/decimal columns. Also facing issue with the scientic number in the column.

DataSet myDataSet = new DataSet();
myDataSet.ReadXml(_filePath);
ultraGrid1.DataSource = myDataSet;

we found that we can define the schema file for the data type. But it would not work in our case, we are reading any xml file and we can't define schema for each file. Do we have any other alternative to solve the below problems with XML datasource of the UltraGrid.

  1. Sorting with Integer/Decimal columns
  2. Scientific numbers in a cell (We want real number instead)

Please refer below screenshot as well as attached xml file.

SampleXmlFile.xml
<?xml version="1.0"?>
<catalog>
   <book id="1">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>-5.89757073860143E-05</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="2">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>2.540581687697042E7</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="3">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>2.540581687697042E7</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="4">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="5">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="6">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="7">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="8">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="9">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="10">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="11">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="12">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

Parents
No Data
Reply
  • 34810
    Offline posted

    Hello Narendra,

    By default, the DataSet.ReadXml method will read your XML into a DataTable and all columns will be interpreted as a String data type. The UltraGrid will see this when you bind your DataSet to the DataSource and so it will generate columns in the grid that believe that their data type is String.

    I have found that you can prevent this by passing in an XmlReadMode flag of InferTypedSchema to the ReadXml method, like so:

    myDataSet.ReadXml(_filePath, XmlReadMode.InferTypedSchema);

    This appears to generate a DataTable within your DataSet that has the correct column types, as this will let the Xml reader try to infer the data type based on what is typed rather than just reading everything as a string.

    Regarding the scientific numbers in your cells, you will need to evaluate these and change what you are giving the UltraGrid if you want the “real number” in this case. Your XML data has scientific numbers in it, and as such, you will receive scientific numbers in the grid unless you change it in the data source before binding it. Perhaps you could loop through your data source and look for scientific numbers and evaluate them to be the full number so that the grid will give you the full number instead?

    Please let me know if you have any other questions or concerns on this matter.

Children
No Data