When I read an xml file into a dataset, if the root node has more than one of each child tag, it doesn't create a table for the root node. Why is this? Is there any way to get it to generate the root table without modifying the xml?
static void Main(string[] args) {
var ds =Load(@"<root>
<Stores>Lambton Quay</Stores>
<Stores>Willis Street</Stores>
</root>");
var ds2 = Load(@"<root>
<t>1</t>
<Stores>Lambton Quay</Stores>
<Stores>Willis Street</Stores>
</root>");
Console.WriteLine("ds table count: {0}", ds.Tables.Count); //1 <- expecting 2
Console.WriteLine("ds2 table count: {0}", ds2.Tables.Count); //2
Console.ReadKey();
}
static DataSet Load(string xml) {
var xd = new XmlDocument();
xd.LoadXml(xml);
var ds = new DataSet();
ds.ReadXml(new XmlNodeReader(xd));
return ds;
}
Edit:
To be clear I want to know why the DataSet ds doesn't have a root
table while ds2 does.
Someone made the decisions:
- if only 1 node exists with a given tag name under a given parent tag, it becomes a column in the table named by the parent tag.
- if more than one such node exists, it becomes a table on its own
- if all child nodes appear more than once and the parent node is the document root, the parent table does not get created
What is the reason for #3?