?
Solved

datatable.readxml

Posted on 2009-02-17
15
Medium Priority
?
804 Views
Last Modified: 2012-06-22
I have a string with the following xml data:
 <entity>
  <name>bhanuug</name>
  <email>bhanuug@gmail.com</email>
  <prov>QC</prov>
  <city>asdsd</city>
  <galleryStatus>0</galleryStatus>
  </entity>

Dim xmlRd As XmlTextReader = New XmlTextReader(Server.HtmlDecode(entity), XmlNodeType.Element, Nothing)
when i try to do datatable.readxml(xmlRd)

it gives me followung exception
"DataTable does not support schema inference from Xml."

Can anyone help me?
0
Comment
Question by:bhanuug
  • 6
  • 5
  • 4
15 Comments
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23665009
How are you declaring your DataTable?
0
 

Author Comment

by:bhanuug
ID: 23665163
i  am decaring it as
dim dtList as new datatable
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23665167
Yeah, read that link I posted, you have to declare the datatable and the columns in the datatable before you can read XML in to it.
0
 

Author Comment

by:bhanuug
ID: 23665186
the xml is going to be generic. So is there a way that the columns gets created when we read from the xml string?
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23665218
Will there be multiple entities, in the XML, or only one at a time?

IE will the data be
column1
Joe
me@test.com
Some City

Or will it be
col1              col2             col3
Joe               Bob             John
a@a.com     b@b.com    c@c.com
0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23665228
actually that should have been

col1       col2
Joe        a@a.com
Bob       b@b.com
John      c@c.com
0
 

Author Comment

by:bhanuug
ID: 23665270
Its going to be

col1       col2
Joe        a@a.com

i always get one row in the xml which i have assign it to table.
here is the ex of the xml string
<entity>
  <name>bhanuug</name>
  <email>bhanuug@gmail.com</email>
  <prov>QC</prov>
  <city>asdsd</city>
  <galleryStatus>0</galleryStatus>
  </entity>

dtList.Columns.Add("ID", GetType(String))
        dtList.Columns.Add("timestamp", GetType(String))
        dtList.Columns.Add("name", GetType(String))
        dtList.Columns.Add("email", GetType(String))
        dtList.Columns.Add("prov", GetType(String))
        dtList.Columns.Add("city", GetType(String))
        dtList.Columns.Add("galleryStatus", GetType(String))
        dtList.Columns.Add("image", GetType(String))
        dtList.Columns.Add("thumb", GetType(String))

Open in new window

0
 
LVL 15

Expert Comment

by:aibusinesssolutions
ID: 23665347
Yeah the only way to create the columns dynamically would be to step through the XML file using an XMLReader before reading the data into the datatable.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1500 total points
ID: 23665830
Yuou can use DataSet.readxml() method. I have tried - it was OK. See my snippet:

The code is in C#. Do you need VB version - I can do?
        private void button2_Click(object sender, EventArgs e)
        {
            string xml = @" <entity>
  <name>bhanuug</name>
  <email>bhanuug@gmail.com</email>
  <prov>QC</prov>
  <city>asdsd</city>
  <galleryStatus>0</galleryStatus>
  </entity>";
            DataSet ds = new DataSet();
 
              XmlTextReader xmlRd= new XmlTextReader(xml, XmlNodeType.Element, null);
 
            ds.ReadXml(xmlRd);
            MessageBox.Show("" + ds.Tables.Count);
            MessageBox.Show("" + ds.Tables[0].Columns.Count);
 
            string columnNames = "";
            foreach (DataColumn col in ds.Tables[0].Columns)
            {
                columnNames += col.ColumnName + Environment.NewLine;
            }
            MessageBox.Show(columnNames);
 
 
        }

Open in new window

0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 23685064
Huh, just wonder - why B for the working and tested solution?
0
 

Author Comment

by:bhanuug
ID: 23685542
If you see my question i already tried the code snippet you sent me and it did not work. i have get the code out. so i used XPathNavigator to get the values individually and assigned it to the datatable. i appritiate for you efforts to help me out. that is the reason i gave B. Sounds fare?
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 23685773
My snippet is very different from your question. I use a DataSet to read your xml and you use DataTable. This is a bit different. If you cut and paste my code it won't give the error you complained about. And it will read the table.

I see you didn't even tried my code. Does not matter - but I don't like to get points for nothing, quite embarrasing :)
0
 

Author Comment

by:bhanuug
ID: 23685979
I did not want to embarras you. i am not sure how to change it from B to A. let me know i will do that..
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 23686480
Ha, no worries... I don't really care about points. I just like to help (at breakes in my main job :)).
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question