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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:59:55+00:00 2026-06-05T17:59:55+00:00

I asked this question earlier and I got very quick and helpful responses: Linq

  • 0

I asked this question earlier and I got very quick and helpful responses: Linq to XML query not picking up any values

I followed the advice as given in the answer marked as correct which worked and allowed me to pick up the ‘Product’ tags. I am now searching within that ‘Product’ tag in order to find values to import into my database and I’m a bit stuck again.

Here is the structure of my XML file:

<?xml version="1.0"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage xmlns="http://www.editeur.org/onix/2.1/reference" release="2.1">
  <Header>
    <FromCompany>MyCo</FromCompany>
    <FromPerson>Joe Bloggs</FromPerson>
    <FromEmail>joe@bloggs.com</FromEmail>
    <SentDate>20120522</SentDate>
  </Header>
  <Product>
    <ProductForm>DG</ProductForm>
    <Title>
      <TitleType>01</TitleType>
      <TitleText>Blogg</TitleText>
    </Title>
    <WorkIdentifier>
      <WorkIDType>15</WorkIDType>
      <IDValue>PI3564231</IDValue>
    </WorkIdentifier>
    <Language>
      <LanguageRole>01</LanguageRole>
      <LanguageCode>eng</LanguageCode>
    </Language>
  </Product>
</ONIXMessage>

Here is the snippet from my code:

        XElement onix = XElement.Load(fs);

        // Get all the product information.
        //
        var products = onix.Descendants().Where(m => m.Name.LocalName == "Product").ToList();

        foreach (var p in products)
        {
            try
            {
                prod.Title = p.Elements("Title").First(t => (t.Element("TitleType").Value == "01")).Element("TitleText").Value;
                //further processing
            }
            catch (Exception exp)
            {
                throw new FileProcessingException("Unable to process product.", exp);
            }
        }

When I search for the Title text as shown above, the exception is caught. The exception is “Object reference not set to an instance of an object.” The item is clearly in the XML file, it just isn’t picking it up right.

I had never seen .Descendants() before toda and I get what it means, but from what I read I should be able to use .Elements() here also?

Can anyone see what could be wrong?

  • 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-05T17:59:57+00:00Added an answer on June 5, 2026 at 5:59 pm

    Well, as I answered your previous question, I think I might help…

    Your document has a namespace (xmlns).
    If you wanna use Elements(“tagName”) or Element(“tagName”), etc, you have to prefix all your TagName By the namespace.

    so your query will work (as mentioned by Habib.OSU in your previous question), if you do

      XNamespace ns = onix.Attribute("xmlns").Value;
      var products2 = onix.Elements(ns + "Product").ToList();
    
      foreach (var p in products2)
      {
            var title = p.Elements(ns + "Title")
            .First(t => (t.Element(ns + "TitleType").Value == "01"))
            .Element(ns + "TitleText").Value;
      }
    

    if you absolutely don’t wanna use the namespace, you have to use Descendants() and Name.LocalName. It’ll give you the same, but… it’s really hard to read.

     var products = onix.Descendants().Where(m => m.Name.LocalName == "Product").ToList();
    
     foreach (var p in products)
     {
    
          var title = p.Descendants().Where(m => m.Name.LocalName == "Title")
             .Descendants()
             .First(m => m.Name.LocalName == "TitleType" && m.Value == "01")
             .Parent
             .Descendants()
             .First(m => m.Name.LocalName == "TitleText")
             .Value;
    
     }
    

    I didn’t put any NULL Check, this would be, of course, something to do.

    Also note that the second query will be less performant, as Descendants() enumerates all subElements of an elements, without taking care of hierarchy. While Elements() will retrieve the direct child Nodes only.

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

Sidebar

Related Questions

This question is very similar to an earlier question I asked ( This Question
I asked this goofy question earlier today and got good answers. I think what
I asked this question earlier, but it got a negative vote, so I'm rewording
I asked this question earlier about business logic and presentation logic, and it got
I asked a question earlier this week and i got this statement as the
I asked this question earlier but I was very vague and a lot of
So I asked this question earlier: CSS box shadow not truly transparent? And I
I asked a question earlier yesterday and got some very good answers for sure.
I asked a question on here earlier and got some fantastic responses. I've since
I asked this question a little earlier today but am not sure as to

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.