Solved

Read an XML string into a dataset /datatable

Posted on 2010-09-15
5
578 Views
Last Modified: 2013-12-16
Hello:  I rarely have to work with XML data, so bear with me.

I am getting an email message where in the body of the message there is the following string:

<?xml version="1.0" encoding="UTF-8"?><telemetry><esn>0-436701</esn><unixTimestamp>1284595879</unixTimestamp><hexPayload>0x02063F000000000000</hexPayload></telemetry>

I need to move the data in the XML string to a datatable / dataset so I can push it along to my database.  I've looked at XMLTextReaders and Linq to XML, but can't quite get it.

Suggestions?

Thanks

RW
0
Comment
Question by:cmh_rwhelan
5 Comments
 

Author Comment

by:cmh_rwhelan
ID: 33688845
Offline now for 12 hours,  
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33688970
You can read the values from the XML with code like the following
        Dim msgBody As String = "<?xml version=""1.0"" encoding=""UTF-8""?><telemetry><esn>0-436701</esn><unixTimestamp>1284595879</unixTimestamp><hexPayload>0x02063F000000000000</hexPayload></telemetry>"

        Dim xdoc = XDocument.Parse(msgBody)

        Dim esn As String = xdoc.<telemetry>.<esn>.Value
        Dim unixTimestamp As String = xdoc.<telemetry>.<unixTimestamp>.Value
        Dim hexPayload As String = xdoc.<telemetry>.<hexPayload>.Value

Open in new window

0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 33689045
Try the following
            string myXml = "<xml version=\"1.0\" encoding=\"UTF-8\"><telemetry><esn>0-436701</esn><unixTimestamp>1284595879</unixTimestamp><hexPayload>0x02063F000000000000</hexPayload></telemetry>";
            myXml = myXml.Substring(myXml.IndexOf("<telemetry>"));
            XmlReader xmlReader = new XmlTextReader(new MemoryStream(ASCIIEncoding.Default.GetBytes(myXml)));
            DataSet ds = new DataSet();

            ds.ReadXml(xmlReader);

Open in new window

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33689252
You can read the xml into a DataSet by invoking ReadXml(string) on the DataSet

http://msdn.microsoft.com/en-us/library/360dye2a.aspx


string strXML = "<?xml version="1.0" encoding="UTF-8"?><telemetry><esn>0-436701</esn><unixTimestamp>1284595879</unixTimestamp><hexPayload>0x02063F000000000000</hexPayload></telemetry>";

DataSet ds = new DataSet();
ds.ReadXml(strXML);

Open in new window

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33689281
@Novice_Novice: If you try your example, It will give several errors (first of all on the creation of your strXML and secondly when you try to read it into a dataset. I do the same thing, only I solved already some problems
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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