Solved

How to pharse XML (XmlElement element)

Posted on 2014-03-10
1
307 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 63

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RegEx with optional part 4 42
Convert Ctime to date time in textfile? 7 33
asp.net mvc , views, hidden values ? 2 16
Why use this lambda? 12 38
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

830 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