Load something other than the root node of an XML file using ReadXML

910 views Asked by At

I'm trying to load an XML file into an embedded database (SQLite) using .NET. The best way I've found to do this is to read the data into a DataTable/DataSet and then use a DataAdapter to Update that DataTable into the database table.

The problem is, the XML file I have to work with has data that isn't in the root node. There's the root node (tables), then a subnode (tableXXX) and all of the data is in the subnode.

When I use ReadXML (for the DataSet object), it simply reads in a single record (containing the name of the subnode table). I want to completely ignore the root node and treat the first subnode as if it is the root node.

How would I go about doing this?

(Or, if there's an easier way to load an XML file to a database, I'd be interested in hearing that as well, although I'm guessing I'll still need to work around this root node issue).

WATYF

1

There are 1 answers

4
I4V On BEST ANSWER
string xml = "<tables><table1><col1>xyz</col1></table1></tables>";

DataSet ds = new DataSet();
ds.ReadXml(XDocument.Parse(xml).Root.Element("table1").CreateReader());

var value = ds.Tables[0].Rows[0]["col1"].ToString(); //<-- xyz

EDIT

  • XDocument is in System.Xml.Linq namespace

  • the code can be as

    ds.ReadXml(XDocument.Parse(xml).Root.Elements().First().CreateReader());