none
Get elements, tags from XML file using C#

    Question

  • Hi!

    Totally new with reading XML strings.

    I need to get certain values from this XML output:

    <getReply>
    <Person SocialSec="xyz" LastName="Doe" FirstName="John" />
    </getReply>

    Just cant figure out on how to get the info from SocialSec, LastName and FirstName.

    Any help appreciated.

    Regards

    Martin Arvidsson, Sweden

    Tuesday, June 18, 2013 1:59 PM

Answers

  • Read the introduction on LINQ to XML LINQ to XML on MSDN if you use .NET 3.5 or later.

    With C# if you have a file you load it with

      XDocument doc = XDocument.Load("file.xml");

    if you have a string with the XML you posted you use

      XDocument doc = XDocument.Parse("<getReply>....</getReply>");

    then you can access elements (like the "Person") element with e.g.

      doc.Root.Element("Person")

    and attributes of the element (like the "SocialSec" attribute value) with e.g.

      doc.Root.Element("Person").Attribute("SocialSec").Value

    Assuming you want to process a couple of elements you can do e.g.

      foreach (XElement person in doc.Descendants("Person"))

      {

         Console.WriteLine("{0} {1} has social security number {2}", person.Attribute("FirstName").Value, person.Attribute("LastName").Value, person.Attribute("SocialSec").Value);

      }


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Marked as answer by Datamartin Tuesday, June 18, 2013 5:41 PM
    Tuesday, June 18, 2013 2:35 PM
  • I showed you in 2 different ways, I will suggest you to go with LinQ query.

     var xElement = XElement.Parse("<getReply><Person SocialSec='xyz' LastName='Doe' FirstName='John' /></getReply>");
    
    // Linq query , preferred one
                var person = from temp in xElement.Elements("Person")
                             select new
                                        {
                                            SocialSec = temp.Attribute("SocialSec").Value,
                                            LastName = temp.Attribute("LastName").Value,
                                            FirstName = temp.Attribute("FirstName").Value,
                                        };
    
    //data in separate variables
                var SocialSec = xElement.Element("Person").Attribute("SocialSec").Value;
                var LastName = xElement.Element("Person").Attribute("LastName").Value;
                var FirstName = xElement.Element("Person").Attribute("FirstName").Value;


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Datamartin Tuesday, June 18, 2013 5:41 PM
    Tuesday, June 18, 2013 2:51 PM

All replies

  • Read the introduction on LINQ to XML LINQ to XML on MSDN if you use .NET 3.5 or later.

    With C# if you have a file you load it with

      XDocument doc = XDocument.Load("file.xml");

    if you have a string with the XML you posted you use

      XDocument doc = XDocument.Parse("<getReply>....</getReply>");

    then you can access elements (like the "Person") element with e.g.

      doc.Root.Element("Person")

    and attributes of the element (like the "SocialSec" attribute value) with e.g.

      doc.Root.Element("Person").Attribute("SocialSec").Value

    Assuming you want to process a couple of elements you can do e.g.

      foreach (XElement person in doc.Descendants("Person"))

      {

         Console.WriteLine("{0} {1} has social security number {2}", person.Attribute("FirstName").Value, person.Attribute("LastName").Value, person.Attribute("SocialSec").Value);

      }


    MVP (XML, Data Platform Development) 2005/04 - 2013/03 My blog

    • Marked as answer by Datamartin Tuesday, June 18, 2013 5:41 PM
    Tuesday, June 18, 2013 2:35 PM
  • I showed you in 2 different ways, I will suggest you to go with LinQ query.

     var xElement = XElement.Parse("<getReply><Person SocialSec='xyz' LastName='Doe' FirstName='John' /></getReply>");
    
    // Linq query , preferred one
                var person = from temp in xElement.Elements("Person")
                             select new
                                        {
                                            SocialSec = temp.Attribute("SocialSec").Value,
                                            LastName = temp.Attribute("LastName").Value,
                                            FirstName = temp.Attribute("FirstName").Value,
                                        };
    
    //data in separate variables
                var SocialSec = xElement.Element("Person").Attribute("SocialSec").Value;
                var LastName = xElement.Element("Person").Attribute("LastName").Value;
                var FirstName = xElement.Element("Person").Attribute("FirstName").Value;


    Thanks & Regards
    Syed Amjad Sr. Silverlight/WPF Developer,
    yahoo : syedamjad6736@yahoo.com, skype : syedamjad.0786.
    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    • Marked as answer by Datamartin Tuesday, June 18, 2013 5:41 PM
    Tuesday, June 18, 2013 2:51 PM