XML and Datasets
Yep, me again with yet another stupid XML question.
I have a chunk of xml data:
<root rname="Directory">
<company name="XYZ Inc.">
<addressLines addressdata="One Abc Way">
<address2 val="his avenue"></address2>
<city cname="Tech city"></city>
<country cname="Neverland"></country>
</addressLines>
</company>
<company name="ABC Inc.">
<addressLines addressdata="One Abc Way">
<address2 val="his avenue">Address Line2</address2>
<city cname="Tech city">Derbyshire</city>
<country cname="Neverland">Nverland</country>
</addressLines>
</company>
</root>
as you can see, quite simple little chunck.
I'm now trying to read this into a dataset using the following code:
string file1;
od1.ShowDialog();
file1 = od1.FileName;
DataSet ds =new DataSet();
FileStream findata =new FileStream(file1, FileMode.Open, FileAccess.Read,FileShare.ReadWrite);
ds.ReadXml (findata, System.Data.XmlReadMode.InferSchema);
findata.Close();
DataTable myDt = ds.Tables[0];
foreach (DataRow rin ds.Tables[0].Rows)
{
MessageBox.Show(r.ToString());
}
When I run this I get only one row returned, anyone got any ideas why?
And if this is a retard question please stick with me, I'm just picking up the whole xml in c# thing.thanks in advance,
Ed.
Ed,
I'm currently reading the book, 'Applied Xml Programming for Microsoft .NET" by Dino Esposito. Like you, I'm new to XML, but, if you plan to use XML extensively, I would highly recommend D. Esposito's book. The following information is from that book.
Anyway, the author has alot to say about Data Sets and XML (and methods of populating Data Sets from XML files). I'll provide info here that seems useful to your circumstances.
"To fill a DataSet object with XML data, you can use one of two methods. (The first method is the one you are using). The second method is to load the data as XML into an instance of the XmlDataDocument class, and then use the XmlDataDocument.DataSet method to fill the DataSet object. The two approaches differ significantly in terms of data fidelity."
"When XmlRead is used and the data is written back as XML, all extra XML information such as white spaces, processing instructions, and CDATA sections is irreversibly lost. This happens because the DataSet relational format simply does not know how to handle information that is meaningful only to the hierarchical model."
"When the DataSet object is filled using an XML document loaded into XmlDataDocument, the DataSet object still contains a simplified and adapted representation of the hierarchical contents but the original XML document is preserved intact."
...There's More,...
"Although ReadXml supports various types of sources -- streams, files, and text readers -- the underlying routine used in all cases reads data using an Xml reader.
The Xml source is read one node after the next until the end is reached. The information read is transformed into a DataRow object that is added to a DataTable object. Of course, the layout of both the DataTable object and the DataRow object is determined based on the schema read or inferred."
...And, there's still more,...
There is information about Inferring Schema (the ReadMode option that you used with ReadXml). This is probably getting kind of tedious,...am I right?
Unfortunately, my conception of schema is almost null. But the author goes on to describe Microsoft Visual Studio using an algorithm for rendering the Xml schema, and then, doesn't really elaborate with useful details.
I'd suspect that your problem is with the way the Xml Reader is assembling your data. If it was me, I'd use the XmlDocument class, and a custom reader that will provide foolproof results. Although, this will involve some coding, the obvious benefit is that you will not have to read through another one of these long, boring, informative replies to your inquiry.