The following parses the data.xml file (see sample below). I can pull the body value just fine but I'm having some trouble with the recipient field. I've looped through other child elements before using this same code but not sure what I'm doing wrong here.
using System; using System.IO; using System.Text; using System.Text.RegularExpressions; using System.Xml.Linq; namespace MessagesRestore { public class ParseXml { public static void ExtractMessages() { string recepients = ""; string body = ""; foreach (XElement level1Element in XElement.Load(@"Data.xml").Elements("Message")) { body = level1Element.Element("Body").Value; //Works foreach (XElement level2Element in level1Element.Elements("Recepients")) { recepients = level2Element.Element("string").Value; //Fails } } } } } data.xml file:
<?xml version="1.0" encoding="utf-8"?> <ArrayOfMessage> <Message> <Recepients> <string>5555555555</string> </Recepients> <Body>blah blah blah</Body> <IsIncoming>false</IsIncoming> <IsRead>true</IsRead> <Attachments /> <LocalTimestamp>130979714416748889</LocalTimestamp> <Sender /> </Message> <Message> <Recepients> <string>5555555555</string> </Recepients> <Body>yaba daba do</Body> <IsIncoming>false</IsIncoming> <IsRead>true</IsRead> <Attachments /> <LocalTimestamp>130979673444601802</LocalTimestamp> <Sender /> </Message> <Message> </ArrayOfMessage> Thank you, Greg
No comments:
Post a Comment