Solved

Read an XML string into a dataset /datatable

Posted on 2010-09-15
5
580 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

713 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