none
Multiple tables from ReadXML RRS feed

  • Question

  •  

    If I have an XML structure like

     

    Code Snippet

    <pets>

      <cats>

        <cat>

          <name>Fluffy</name>

          <breed>Calico</breed>

        </cat>

        <cat>

          <name>Mittens</name>

          <breed>Tabby</breed>

        </cat>

      <fishes>

        <fish>

          <name>Finny</name>

          <watertype>Salt</watertype>

        </fish>

        <fish>

          <name>Goldie</name>

          <watertype>Fresh</watertype>

        </fish> 

      </fishes>

    </pets>

     

     

    How can I get an ADO.Net DataSet to read it into two Tables (cats, and fishes)? Right now when I use ReadXml I get an error telling me there is more than one root element even though pets is the only root element.

    Monday, August 25, 2008 5:11 PM

Answers

  •  

    So it appears the way to do it is the following:

     

    Code Snippet

    System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();

    settings.ConformanceLevel = System.Xml.ConformanceLevel.Fragment;

    System.IO.Stream stream;

    stream = null; //Get your XML from file, memory or whatever here

    System.Xml.XmlReader reader = System.Xml.XmlReader.Create(stream, settings);

    System.Data.DataSet ds = new System.Data.DataSet();

    ds.ReadXml(stream);

     

     

     

    Then you will have a DataSet with 4 tables (0-3). Tables 1 and 3 are the ones with the actual data for the cat and fish.

    Tuesday, August 26, 2008 5:50 PM