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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:13:03+00:00 2026-05-22T23:13:03+00:00

I have two XELEMENTS XR and XTemp . MY XR Contains : <result xmlns=http://abc.com/test>

  • 0

I have two XELEMENTS XR and XTemp.

MY XR Contains :

     <result xmlns="http://abc.com/test">
           <report>
            <unit unit_number="1" id="S1">
                 <classification>Subject</classification>
                 <claim_reported count="0" status="No claims reported" />
            </unit>
           <unit unit_number="2" id="S2">
                <classification>Subject</classification>
                <claim_reported count="0" status="No claims reported" />
           </unit>
        <unit unit_number="3" id="V1">
          <classification>Vehicle</classification>
          <claim_reported count="1" status="Claims reported" />
        </unit>
    ......
     </report>
</result>

My XTemp contains :

 <result xmlns="http://abc.com/test">
   <report>
        <unit unit_number="1" id="S3">
             <classification>Subject</classification>
             <claim_reported count="1" status="Claims reported" />
        </unit>
       <unit unit_number="2" id="S4">
            <classification>Subject</classification>
            <claim_reported count="4" status="Claims reported" />
       </unit>
      <unit unit_number="3" id="V2">
         <classification>Vehicle</classification>
         <claim_reported count="0" status="No claims reported" />
      </unit>
      <unit unit_number="3" id="V3">
         <classification>Vehicle</classification>
         <claim_reported count="2" status="Claims reported" />
      </unit>
         ......
    </report>
 </result>

Trying to extract all the <unit> elements from XTemp based on a certain condtion and add them to XR.

The condition is : get all the <unit> elements whose id begins with “V”.

Below code was the best I was able to come up with.

But, I get Object reference not set to an instance Error.

XR.Descendants(xmlns + "unit").LastOrDefault().AddAfterSelf(XTemp.Descendants(xmlns + "unit")
  .Where(x => x.Element(xmlns + "unit").Attribute("id").Value.ToUpper().Contains("V")));

Thanks in advance

BB

  • 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-22T23:13:04+00:00Added an answer on May 22, 2026 at 11:13 pm

    Your problem is in this bit of code:

    .Where(x => x.Element(xmlns + "unit").Attribute("id")...
    

    At that point in the query you’re already querying over unit elements. A unit doesn’t contain a nested unit, which is why you get a NullReferenceException.

    Change it to: .Where(x => x.Attribute("id")...

    Personally, I find your query hard to read. Firstly, LastOrDefault means you potentially expect a null value to exist, yet you’re forging ahead with the rest of the query without addressing it. Secondly, you don’t need to find the last unit element and AddAfterSelf. Instead you can directly Add to the report element and the elements will be appended to the end:

    var query = xtemp.Descendants(xmlns + "unit")
                     .Where(e => e.Attribute("id").Value.StartsWith("V", StringComparison.InvariantCultureIgnoreCase));
    
    xr.Element(xmlns + "report").Add(query);
    

    Also, notice how I checked for “V” while ignoring the case. This is considered best practice as opposed to using ToUpper.


    EDIT: based on the comments, here is a solution to sort all the unit nodes while preserving the existing order of non-unit nodes. This code takes into account whether node occurs before and after existing nodes, or no other nodes at all.

    Some assumptions are:

    • XR always has unit nodes
    • IDs follow the existing letter followed by a number format.
    • unit nodes are kept together and are not interspersed between all other types of nodes.

    The first two assumptions are trivial issues to code for if they’re incorrect.

    var query = xtemp.Descendants(xmlns + "unit")
                     .Where(e => e.Attribute("id").Value.StartsWith("V", StringComparison.InvariantCultureIgnoreCase));
    
    var units = xr.Element(xmlns + "report").Elements(xmlns + "unit");
    var beforeUnit = units.First().ElementsBeforeSelf().FirstOrDefault();
    var afterUnit = units.Last().ElementsAfterSelf().FirstOrDefault();
    
    // order based on ID starting letter, then integer value
    var orderedUnits = units.Concat(query)
                            .OrderBy(e => e.Attribute("id").Value[0])
                            .ThenBy(e => int.Parse(e.Attribute("id").Value.Substring(1)))
                            .ToList();
    
    // remove original unit nodes
    units.Remove();
    
    // add ordered units based on their original position
    if (beforeUnit != null)
    {
        beforeUnit.AddAfterSelf(orderedUnits);
    }
    else if (afterUnit != null)
    {
        afterUnit.AddBeforeSelf(orderedUnits);
    }
    else
    {
        xr.Element(xmlns + "report").Add(orderedUnits);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am facing one problem i have two xml files. Plugins.xml <SolutionProfile xmlns=http://schemas.microsoft.com/pag/cab-profile> <Modules>
I have two pages in WPF. Page A contains all of my code. Page
For example, I have two elements in an enum. I would like the first
I have two select elements, A and B: when A's selected option changes, B's
I have two span elements that I would like to stay on the same
I have two div elements that are twins (i.e. their dimensions and contents are
I have two div -elements, the top one has the height of 40% and
I want to be able to have two Haml elements on the same line.
I have two sets of elements with (sometimes) corresponding rel and id attributes: <a
I have two major xml elements: courses and CRNs <courses> <course credits=3 courseNum=COMP1950 name=Intermediate

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.