Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read an XML string into a dataset /datatable

Posted on 2010-09-15
5
Medium Priority
?
586 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 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

783 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