简体   繁体   中英

DataSet.ReadXml not getting all data

I have a relatively simple xml file that I would like to use to fill a DataSet. I am trying the following code

using (DataSet ds = new DataSet()) 
{
    ds.ReadXml(Server.MapPath("xmlFileName.xml"));
}

But my dataset ends up with only one row containing the first node of the file.

I have tried other methods such as

XmlReader xmlFile = XmlReader.Create("xmlFileName.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);

And

FileStream fsReadXml = new FileStream("xmlFileName.xml", FileMode.Open);
XmlTextReader xmlReader = new System.Xml.XmlTextReader(fsReadXml);
newDataSet.ReadXml(xmlReader, XmlReadMode.ReadSchema);

Both of which result in empty datasets.

I can't post the entire xml file, but its format is essentially

<service_orders count="70">
   <service_order order_number="1111" id="111111">
     <customer>
        <customer_id>55555</customer_id>
        <first_name>John</first_name>
        <last_name>Doe</last_name>
        <email>JohnDoe@gmail.com</email>
        <phone1>55555555</phone1>

The first method I mentioned only generates two columns from this "service_order_id" and "count" with values 0 and 70 respectively. It seems like it's only hitting the first node?

So I'm not sure what I'm doing wrong with these methods. Is the xml file not formatted properly? Do I somehow need to make it go deeper into the nodes? Is there any way I can specify which nodes to hit?

Any help would be appreciated, Thank you

I realized I had forgotten that DataSets hold multiple tables, and that I was only looking at the first one which contained the root. Though this did help me understand how the parser navigates the xml tree. Only leaf elements (with no children) get added to the tables, all other elements get tables of their own in the dataset.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM