C#: How does DataSet.readXML( “filepath” ) work wi

2019-07-17 03:09发布

问题:

I've got an xml file formatted like this:

<items>
<item>
    <itemProperty1>Propeterty1</itemProperty1>
    <itemProperty2>Propeterty2</itemProperty2>
    <propertyWithSubProperties1>
       <subprop1>subProp1</subProp1>
       <subprop2>subProp2</subProp2>
       <deeperPropertyWithSubProperties1>
            <deeperSubProperty1>data</deeperSubProperty1>
            <deeperSubProperty2>data2</deeperSubProperty2>
       </deeperPropertyWithSubProperties1>
    </propertyWithSubProperties1>
</item>
...More items
</items>

I'm trying to use a dataset in c# to read in this datafile like this:

DataSet dataSet = new DataSet();
dataSet.ReadXml(fileName);

I can access the first two items no problem like this:

firstProperty = dataSet.Tables[0].Rows[i][0].ToString();
secondProperty = dataSet.Tables[0].Rows[i][1].ToString();

But nothing I'm completely baffled as to how to get at the information in the other items because they are a level or more deeper. My usual google searches turn up nothing of use.

回答1:

Your DataSet will contain three tables:

  • dataSet.Tables[0]:
    Columns itemProperty1, itemProperty2 and item_Id.

  • dataSet.Tables[1]:
    Columns subProp1, subProp2, propertyWithSubProperties1_Id and item_Id.

  • dataSet.Tables[2]:
    Columns deeperSubProperty1, deepSubProperty2 and propertyWithSubProperties1_Id .

Tables[0] is linked to Tables[1] using the auto-generated item_Id columns.

Tables[1] is linked to Tables[2] using the auto-generated propertyWithSubProperties1_Id columns.



回答2:

Have you tried using XmlTextReader?

http://msdn.microsoft.com/en-us/library/system.xml.xmltextreader.aspx

Or LINQ to XML?

http://msdn.microsoft.com/en-us/library/bb387098.aspx



回答3:

A DataSet is probably not the ideal data structure for this nested hierarchy. Have you considered reading the XML directly? This can be done as follows:

XmlDocument d = new XmlDocument();
d.Load("XmlFile1.xml");

XmlElement items = d.DocumentElement;//(XmlElement)d.GetElementById("items");
XmlElement item = (XmlElement)items.ChildNodes[0];
XmlElement itemProperty1 = item["itemProperty1"]; 
XmlElement itemProperty2 = item["itemProperty2"];
XmlElement propertyWithSubProperties1 = item["propertyWithSubProperties1"];
XmlElement subProp1 = propertyWithSubProperties1["subprop1"];
XmlElement subProp2 = propertyWithSubProperties1["subprop2"];
XmlElement deeperPropertyWithSubProperties1 = propertyWithSubProperties1["deeperPropertyWithSubProperties1"];
XmlElement deeperSubProperty1 = deeperPropertyWithSubProperties1["deeperSubProperty1"];
XmlElement deeperSubProperty2 = deeperPropertyWithSubProperties1["deeperSubProperty2"];

Console.WriteLine(itemProperty1.InnerText);
Console.WriteLine(itemProperty2.InnerText);
Console.WriteLine(subProp1.InnerText);
Console.WriteLine(subProp2.InnerText);
Console.WriteLine(deeperSubProperty1.InnerText);
Console.WriteLine(deeperSubProperty2.InnerText);
Console.ReadKey();

This is not the "best" way to deal with XML documents, but it is probably more analogous to the DataSet approach you are attempting. Other options are to use the XmlTextReader or LINQ to XML (as suggested by @TomS).