Solved

How to pharse XML (XmlElement element)

Posted on 2014-03-10
1
302 Views
Last Modified: 2014-03-11
Hi

I have a variable which is

(XmlElement element)

I will to test element by assigning it

element = "<?xml version="1.0" encoding="UTF-8"?><note><to> Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>"


the problem I get is the XML has to be pharsed, does anyone know how I can get this to work?

thanks
0
Comment
Question by:mousemat24
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39918447
Hi mousemat24;

See if something like this is what you are looking for.

using System.Xml.Linq;

string element = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to> Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
           
XDocument xdoc = XDocument.Parse(element);

var parsed = (from x in xdoc.Descendants("note")
              select new
              {
                  To = x.Element("to").Value,
                  From = x.Element("from").Value,
                  Heading = x.Element("heading").Value,
                  Body = x.Element("body").Value
              }).ToList();
  
             
foreach (var p in parsed)
{
    Console.WriteLine("TO : {0}\nFrom : {1}\nHeading : {2}\nBody : {3}", p.To, p.From, p.Heading, p.Body);
}             

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

862 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

26 Experts available now in Live!

Get 1:1 Help Now