Solved

Read an XML string into a dataset /datatable

Posted on 2010-09-15
5
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Office 1 66
TFS 2015 Access denied. 1 34
Convert VB web project to C# 3 65
Data Analysis 7 59
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

734 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