Solved

Read an XML string into a dataset /datatable

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now