11

I have the following XML file saved:

<E:Events xmlns:E="Event-Details">
   <Date>12/27/2012</Date>
   <Time>‎11:12 PM</Time>
   <Message>Happy Birthday</Message>
</E:Events>

I am using XElement to load the above XML file. I want to get the Element Value of Date, Time and Message i.e. 12/27/2012, ‎11:12 PM and Happy Birthday. How can I retrieve these values. I have searched a lot on this but could not find anything.

Any help appreciated...

0

1 Answer 1

20

Have you just tried getting the element from your XElement node?

XElement.Element(" < element name >");

will return the nodes you need.

Try the code below:

string text = "<E:Events xmlns:E=\"Event-Details\"><Date>12/27/2012</Date><Time>‎11:12 PM</Time><Message>Happy Birthday</Message></E:Events>";
XElement myEle = XElement.Parse(text);
Console.WriteLine(myEle.Element("Date").Value);
Console.WriteLine(myEle.Element("Time").Value);
Console.WriteLine(myEle.Element("Message").Value);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I am able to retrieve the values, but now I am facing one more issue. I have a MainPage.xaml UI, and I try to assign these retrieved values on the UI, like for e.g. txtMessage = myEle.Element("Message").Value; But this throws a null reference exception. Any idea why??..
It would be better to post that a separate question, please post your xaml and the relavant code behind where you are trying to assign the values. Also, if my answer helped you please mark it as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.