Data from XML file to Dataset

L

lgbjr

Hi All,

I need some help getting data from an XML file into a dataset. The XML file
has more data in it than I actually need. So, what I think I need to do is
to create a dataset with the tables and columns that I need, then use the
dataset.ReadXml to put the data into the tables.

So, I've created a dataset, created 3 tables, each with 2 columns. I have
three dataviews on a form linked to these tables. So far, this is what I'm
doing:

Private Sub Load_XML_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Load_XML.Click
Dim result As DialogResult = XML_Picker.ShowDialog
If result = Windows.Forms.DialogResult.OK Then
Dim fsReadXml As New System.IO.FileStream(XML_Picker.FileName,
System.IO.FileMode.Open)
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
DataSet1.ReadXml(myXmlReader, XmlReadMode.IgnoreSchema)
myXmlReader.Close()
End If
End Sub

I have an XSLT file for the XML file, but I'm not sure how to use it or if I
need it to aid in getting this data into the dataset. I'm sure I'm missing
100 steps in doing this, but I don't know what they are. All of the examples
I have looked at show how to write an XML file from a dataset, then read
that XML file into a new dataset. Unfortunately, none of the samples create
a file that looks anything like the XML file I'm starting with, so I'm a bit
lost.

TIA

Lee
 
P

Peter Huang [MSFT]

Hi

To load an XML into dataset, the xml file needed to be compatible with
DataSet Schema.
That is to say the XML file's structure will be similar with the xml file
we call WriteXML on a dataset.
So we need to write to write a special XSLT to do the convert, or convert
it manually.

Alternatively, we can use the XML DOM to parse the XML node one by one and
insert them into datatables in the dataset using coding.

e.g.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"..\..\Test.xml");
foreach(XmlNode xn in xmlDoc.ChildNodes)
{
Debug.WriteLine(xn.Name);
foreach(XmlNode xnl in xn.ChildNodes)
Debug.WriteLine("\t"+xnl.Name);
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

c j anderson, mcp

at the basic level, you
0) create a DataSet object, dset
1) use the method ReadXmlSchema of the instance dset
2) create an XmlDataDocument from the dset, xdd
3) use the Load method of the xdd object
4) bind the data

here's a quick snippet:
Dim dset As New DataSet()
dset.ReadXmlSchema("mySchema.xsd")
Dim xdd as New XmlDataDocument(dset)
xdd.Load("myData.xml")
myDataGridView.DataSource = dset
myDataGridView.DataMember = "myRowName"
 
C

Cor Ligthert

Hi,

For those the ones who search this newsgroup and use VBNet the sample from
Peter translated

\\\
Dim XmlDoc as New XmlDocument
xmlDoc.Load("...\..\Test.xml")
For each xn as XmlNode in xmlDoc.ChildNodes
Debug.WriteLine(xn.Name)
for each xnl as XMLNode in xn.ChildNodes
Debug.WriteLine(VBTab & xnl.Name)
next for
next for
///

Never mind Peter I was just

:))))))

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top