Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:39:44+00:00 2026-06-03T06:39:44+00:00

I Want to Parse XML with Linq. Here is My XML Data. <?xml version=1.0

  • 0

I Want to Parse XML with Linq.

Here is My XML Data.

 <?xml version="1.0" encoding="utf-8" ?>
    <people>
      <person>
        <firstname>Kate</firstname>
        <lastname>Smith</lastname>
        <Address>Address</Address>
        <Address>Address2</Address>
        <Address>Address3</Address>
        <age>27</age>
      </person>
      <person>
        <firstname>Tom</firstname>
        <lastname>Brown</lastname>
        <Address>Address4</Address>
        <Address>Address5</Address>
        <Address>Address6</Address>
        <age>30</age>
      </person>
      <person>
        <firstname>Tim</firstname>
        <lastname>Stone</lastname>
        <Address>Address7</Address>
        <Address>Address8</Address>
        <Address>Address9</Address>
        <age>36</age>
      </person>
      <person>
        <firstname>Ann</firstname>
        <lastname>Peterson</lastname>
        <Address>Address10</Address>
        <Address>Address11</Address>
        <Address>Address12</Address>
        <age>27</age>
      </person>
    </people>

public class XmlParser
    {
        List<Person> List = new List<Person>();
        public XmlParser()
        {
            XDocument loadedData = XDocument.Load("Persons.xml");
            var data = (from query in loadedData.Descendants("person")
                select new Person
                {
                    FirstName = (string)query.Element("firstname"),
                    LastName = (string)query.Element("lastname"),
                    //Address = (List<string>)query.Element("Address"), // I Want To Load All Address Into My List
                    Age = (int)query.Element("age")
                }).ToList();
            List.AddRange(data);
        }

        public class Person
        {
            string firstname;
            string lastname;
            List<string> address;
            int age;

            public string FirstName
            {
                get { return firstname; }
                set { firstname = value; }
            }

            public string LastName
            {
                get { return lastname; }
                set { lastname = value; }
            }

            public List<string> Address
            {
                get { return address; }
                set { address = value; }
            }

            public int Age
            {
                get { return age; }
                set { age = value; }
            }

        }
    }

Can Anyone Please Help Me How To Add Adrress Into List in LINQ Query. As You Can See There Are Many Address Nodes Under The Person. So How I Can Add All Address into My List Object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:39:45+00:00Added an answer on June 3, 2026 at 6:39 am

    One way to get the list of elements would be to nest another linq query in your original linq query.

    var loadedData = XDocument.Load("Persons.xml");
    var data = (from query in loadedData.Descendants("person")
                select new Person
                {
                    FirstName = (string)query.Element("firstname"),
                    LastName = (string)query.Element("lastname"),
                    Address = (from address in query.Elements("Address") 
                               select address.Value).ToList(),
                    Age = (int)query.Element("age")
                });
    

    This pattern occurs all the time in linq to xml so i would remember it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a C# object from hierarchical XML data usinq LINQ. I
I have this xml. <?xml version=1.0 encoding=utf-8?> <feed xml:lang=el-gr xmlns=http://www.w3.org/2005/Atom> <title type=text>name.gr</title> <updated>2011-07-23</updated> <link
I want to parse some large XML file using linq to XML but I
I want to parse xml files that have elements like these: <element>&amp</element> <element>&amp;</element> But
I want to parse XML in bash and xpath could do it. (To get
I want to parse a XML document once - at Rails application startup. It
I want to sax-parse in nokogiri, but when it comes to parse xml element
I want to parse a large XML file and I have two options: Perl
I want to parse the following XML document to resolve all entities in it:
I want to parse a couple of thousands XML-files from a website(I have permission)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.