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

  • SEARCH
  • Home
  • 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 534023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:34:07+00:00 2026-05-13T09:34:07+00:00

I have following xml which same child element (Filed) and I want to get

  • 0

I have following xml which same child element (Filed) and I want to get value from each Child element.

Sameple XML

 <root xmlns="">
    <books cat="F1" ISBN="01F187597" genre="Programming">
      <Field name="Title" val="XML" /> 
      <Field name="Publish Date" val="20010424" /> 
      <Field name="Price" val="43.00" /> 
    </books>
 </root>

Code

  XDocument xdoc = XDocument.Load("c:\\test6.xml");

  var booksData = from book in xdoc.Descendants("root")
                  //I guess create this and do something with it
                  // let fieldElements = book.Descendants("Field")
                  select new Book
                  {
                         cat = book.Element("books").Attribute("cat").Value
                         ,ISBN = book.Element("books").Attribute("ISBN").Value
                         ,genre = book.Element("books").Attribute("genre").Value
                         ,Price = "?"
                         ,PublishDate="?"
                         ,Title="?"
                   };

Book Class

 public class Book
  {
    public string cat {get;set;}
    public string ISBN {get;set;}
    public string genre {get;set;}
    public string Title {get;set;}
    public string PublishDate {get;set;}
    public string Price { get; set; }
  }
  • 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-05-13T09:34:08+00:00Added an answer on May 13, 2026 at 9:34 am

    I agree with Dennis xdoc.Descendants(“root”) doesn’t really do anything.

    I modify code so it will check attrbiute exists or not also how you can use lambda expression.

      var booksData = from book in xdoc.Descendants("books")
                                select new Book
                                {
                                    cat = book.Attribute("cat").Value,
                                    ISBN = book.Attribute("ISBN").Value,
                                    genre = book.Attribute("genre").Value,
    
                                    //Dennis Code 
    
                                   Price = (from childField in book.Elements()
                                             where childField.Attribute("name").Value == "Price"
                                             select childField.Attribute("val").Value).SingleOrDefault(),
    
                                    //same as Dennis code but using lambda expression
    
                                    PublishDate = book.Elements("Field").Where(p => p.Attribute("name").Value == "Publish Date")
                                                  .Select(p => p.Attribute("val").Value).SingleOrDefault(),
    
                                     // Check if element exists or not 
                                     //if not exists it will insert empty string
    
                                   Title = book.Elements("Field").Any( p => p.Attribute("name").Value == "Title")
                                            ? book.Elements("Field").Where(p=> p.Attribute("name").Value == "Title")
                                              .Select(p => p.Attribute("val")== null
                                                      ? string.Empty
                                                      : p.Attribute("val").Value
                                                      ).Single()
                                            : string.Empty                                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following xml and I want to fetch the value of node which
I have XML in the following format which I want to reformat: <blocks> <!--
I have the following xml document. I am trying to select any child which
I have defined an XMLList which gets its data from the following XML file:
I have the following thing to achieve in Android(newbie). I have an xml which
I have an xml document which consists of a number of the following: -
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>
I have the following code which outputs an object to an XML file: using
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have the following snippet in my pom.xml (Full pom attached below which can

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.