XML String Doesn't Load Into DataSet Correctly

S

Scott M.

Why would this string of XML (when loaded into a DataSet) not produce a
DataTable with 2 columns? I am getting a DataTable with just one column
(ProductName). When I try to access the second column in the DataTable
(ds.Tables(0).Columns(1)), I get "Cannot find column 1". By the way, there
are no null values at all in the DataReader (dr).

'prodData is a StringBuilder
prodData.Append("<ProductData>")
If dr.HasRows Then
Do While dr.Read
prodData.Append("<ProductName>" & CType(dr.Item("Name"), String)
& "</ProductName>")
prodData.Append("<ProductID>" & CType(dr.Item("PartNumber"),
String) & "</ProductID>")
Loop
End If
prodData.Append("</ProductData>")

The XML string generated by this code is:

<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.MyCompany.com/WorkflowLayer">
<ProductData>
<ProductName>Anod Nuke</ProductName>
<ProductID>AAN001</ProductID>
<ProductName>Avid 1D 20 Vbrakes</ProductName>
<ProductID>AVD001</ProductID>
...
...
...
<ProductName>World Class V-Adapters</ProductName>
<ProductID>WDC001</ProductID>
<ProductName>WTB Velociraptors</ProductName>
<ProductID>WTB001</ProductID>
</ProductData>
</string>

Any ideas?
 
R

Robbe Morris [C# MVP]

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<ProductData>
<ProductName>Anod Nuke</ProductName>
<ProductID>AAN001</ProductID>
</ProductData>
<ProductData>
<ProductName>Joe Blow</ProductName>
<ProductID>AAN002</ProductID>
</ProductData>
</NewDataSet>

--
2004 and 2005 Microsoft MVP C#
Robbe Morris
http://www.masterado.net

Earn $$$ money answering .NET Framework
messageboard posts at EggHeadCafe.com.
http://www.eggheadcafe.com/forums/merit.asp
 
S

Scott M.

I have tried this and it does not change the result. I get a DataSet that
contains a DataTable that contains just one DataColumn. I have also tried
adding yet another XML tag around what you show below and still the same
result.
 
C

Cor Ligthert

Scott,

Probably I don't see what you want, however when ProductData is the table
and ProductName and ProductId are the columns, than I would expect the least
that it has to be like this.
'prodData is a StringBuilder
prodData.Append( said:
Do While dr.Read
prodData.Append( said:
prodData.Append("<ProductName>" & CType(dr.Item("Name"),
String) & "</ProductName>")
prodData.Append("<ProductID>" & CType(dr.Item("PartNumber"),
String) & "</ProductID>")
prodData.Append( said:
prodData.Append( said:

I hope this helps,

Cor
 
R

Rogas69

Hi, try to use
dataXMLSet.ReadXml(prodData.ToString(), XmlReadMode.InferSchema);

HTH

Peter
 

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

Top