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 6728933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:13:43+00:00 2026-05-26T10:13:43+00:00

I have an xml file that returns a set of elements that are unique

  • 0

I have an xml file that returns a set of elements that are unique by a attribute value. This presents a problem, as I can not select a node by its name:

<doc>
    <float name="score">1.2873721</float>
    <arr name="2_category">
        <long>3021</long>
    </arr>
    <arr name="ATR_FamilyName">
        <str>Some Cookbook </str>
    </arr>
    <arr name="ATR_IsFamily">
        <str>0</str>
    </arr>
    <arr name="ATR_SellPrice">
        <str>49.95</str>
    </arr>
    <arr name="ATR_VendorId">
        <str>ABC</str>
    </arr>
    <arr name="ATR_VendorName">
        <str>WROX</str>
    </arr>      
</doc> 

I am using linq to populate a "Product" class. I am able to select the elements by position, however this becomes a problem if the node doesn’t exist. Is there a way to select a node based on the value of its attribute? In the below example, can I get the arr node if the @name attribute = "ATR_FamilyName"? In xpath it would be:

doc/arr[@name = 'ATR_FamilyName']/str

here is my linq to xml query:

var query = from rt in results
    where (String)rt.Descendants().ElementAt(5).Element("str").Value == "0"
    select new Product.Product
        {
            FamilyName = (String)rt.Descendants().ElementAt(3).Value
            // doc/arr[@name = 'ATR_FamilyName']/str - select Family Name is arr/@name 'ATR_FamilyName'                              
            MorePropertiestoset....                              
        };
  • 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-26T10:13:44+00:00Added an answer on May 26, 2026 at 10:13 am

    Like AS-CII’s answer, but without using a query expression (except for the outer one), and with the cast for XAttribute, and selecting the str element value inside an anonymous type:

    select new Product.Product
    {
        FamilyName = rt.Descendants("arr")
                       .Where(x => (string) x.Attribute("name") == "ATR_FamilyName")
                       .Select(x => (string) x.Element("str"))
                       .FirstOrDefault(),
        MorePropertiesToSet....                              
    }; 
    

    Note that the use of a cast for the result of the call to Attribute("name") means that if there are any elements which don’t have the attribute, the cast will result in a null reference (which isn’t equal to the string literal). If you use the Value property, you’ll get an exception. Sometimes an exception may be better – if that indicates that the data is fundamentally broken and you want to find out about it rather than just not match the value.

    (The same is true for the cast of the XElement to string.)

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

Sidebar

Related Questions

I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have an xml file with elements that look like this (abbreviated for clarity):
I have a C# class method that return a xml document not file. How
I have a simple xml file that I'm looping over to set a ValueObject
I have a XML file like this: <Document> <Tests> <Test> <Name>A</Name> <Value>1</Value> </Test> <Test>
So I have this pretty large XML file (40MB) that I'll have to repeatedly
I have a XML file that goes like this... (the XML file was taken
I have an XML file that encodes a directed acyclic graph (DAG) that represents
I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
I have an XML file that has a number of nodes, each of which

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.