Solved

How to pharse XML (XmlElement element)

Posted on 2014-03-10
1
297 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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

15 Experts available now in Live!

Get 1:1 Help Now