views:

156

answers:

5

Question: Using VB.NET/C#, is it really not possible to read the below XML in a dataset without significant work ?

I tried

oDataSet.ReadXml(strFileName)<BR>

and

Dim oDataSet As System.Data.DataSet = New System.Data.DataSet
Dim strLocation As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
strLocation += System.IO.Path.DirectorySeparatorChar + "filename.xml"
oDataSet.ReadXml(strLocation)

But all this is doing is putting ONE row with one cell with content "2010-02-12T10:33:39" in my dataset...

This is driving me crazy...

This is the XML file:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot generated="2010-02-12T10:33:39">
<Employee>
    <MI_KZ>HKBZV</MI_KZ>
    <MI_Name>Doe</MI_Name>
    <MI_Vorname>John</MI_Vorname>
    <MI_Nummer>70642860</MI_Nummer>
    <MI_DatumVon>2010-02-11T10:45:37</MI_DatumVon>
    <MI_DatumBis>2010-03-13T00:00:00</MI_DatumBis>
    <AP_Bezeichnung>5-B-03</AP_Bezeichnung>
    <KOE_Code>FHBM</KOE_Code>
    <KST_Code></KST_Code>
    <KST_Kurz><![CDATA[]]></KST_Kurz>
  </Employee>
  <Employee>
    <MI_KZ>EX2FC</MI_KZ>
    <MI_Name>Doe</MI_Name>
    <MI_Vorname>Judith</MI_Vorname>
    <MI_Nummer>70642680</MI_Nummer>
    <MI_DatumVon>2010-02-10T14:12:56</MI_DatumVon>
    <MI_DatumBis>2010-06-01T00:00:00</MI_DatumBis>
    <AP_Bezeichnung>Gotth.</AP_Bezeichnung>
    <KOE_Code>UEU</KOE_Code>
    <KST_Code></KST_Code>
    <KST_Kurz><![CDATA[]]></KST_Kurz>
  </Employee>
  <Employee>
    <MI_KZ>EX0GW</MI_KZ>
    <MI_Name>Testname</MI_Name>
    <MI_Vorname>Testprename</MI_Vorname>
    <MI_Nummer>70038630</MI_Nummer>
    <MI_DatumVon>2004-05-11T00:00:00</MI_DatumVon>
    <MI_DatumBis>2010-08-16T00:00:00</MI_DatumBis>
    <AP_Bezeichnung>SempSee</AP_Bezeichnung>
    <KOE_Code>KFMP</KOE_Code>
    <KST_Code></KST_Code>
    <KST_Kurz><![CDATA[]]></KST_Kurz>
  </Employee>
</dataroot>
+1  A: 

The oDataSet.ReadXml needs the XML to be in a particular format it seems. Try saving a dataset into an XML file. I am sure, it will be different. From the XML, it is seen that the Table name is missing.

Kangkan
Yea, I know it would work if it weren't for dataroot, but I can't change the XML I get for import...
Quandary
+5  A: 

Datasets can have more than one table you know... try looking at a different table.

Beyond that, the ReadXml() method is really intended for reading xml that was written out with another Dataset's WriteXml() method.

To read arbitrary xml, you should use the xsd.exe program that ships with Visual Studio on a sample xml file. The first pass of this will create a *.xsd schema file for your xml. Then run the tool again on this xsd schema file with the correct options (sorry, don't have visual studio on this machine to check right now) and it will create either a typed dataset, C# class file, or VB.Net class file that you can use to "deserialize" your xml data.

Joel Coehoorn
Oh, lol, table 1 instead of 0 ;-))
Quandary
+1  A: 

There is no problem with your code. When you read the XML it imports 2 tables into the dataset, the dataroot and the Employee-table.

You can see this if you set a breakpoint after .ReadXML and hover with the mouse over the variable oDataset and shoose to open up the dataset debugger visualizer.

alt text

Iterate through the Emplyee-table:

    For Each person As DataRow In oDataSet.Tables("Employee").Rows
        MsgBox(person.Item("Mi_name"))
    Next
Stefan
+2  A: 

It depends on what you mean by "significant work". Given that the XML isn't in the expected format, I don't think it's at all unreasonable that it doesn't do what you want it to. However, it should be pretty easy to transform the original XML into the appropriate format - either using XSLT or by just reading it into a DOM, shifting everything around appropriately, and then saving that out (possibly to memory). I wouldn't personally call that "significant work".

Jon Skeet
A: 

Use Linq to XML (.Net 3.5 and above) and it's much easier - especially if you're using VB .Net, as this has the added advantage of XML Literals.

Brett Rigby