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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:45:51+00:00 2026-05-25T13:45:51+00:00

I want to select XML elements as following. Please know Data and Party are

  • 0

I want to select XML elements as following. Please know Data and Party are classes. Can someone help how to achieve following:

select new Data
{
Party.Name = xElem.Element(“Name”).Value,
Party.PostBox = xElem.Element(“PostBox”).Value,
}

With current code, I cannot access properties of Party.

static void Main(string[] args)
{
     XDocument doc = XDocument.Load(@"c:\test.xml");
     var q = from xElem in doc.Descendants("Party")
         where (int)xElem.Attribute("ID") == 1
         select new Data
         {

         };
}

public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party = new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

@Jon Skeet: Following is the sample code. I get “Object reference not set to an instance of an object” error at runtime only.

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}

@Jon Skeet: Following is the sample code. I get “Object reference not set to an instance of an object” error at runtime only.

static void Main(string[] args)
{
    XDocument doc = XDocument.Load(@"c:\test\data.xml");
    var props = from xElem in doc.Descendants("Party")
        where (int)xElem.Attribute("ID") == 1
        select new Data
        {
              Party =
              {
                  Name = xElem.Element("Name").Value.ToString(),
                  PostBox = xElem.Element("PostBox").Value.ToString(),
                  Tax =
                  {
                     CompanyID = xElem.Element("Tax").Element("CompanyID").Value.ToString()
                  }
              }
        }
}


public class Data
{
    public Party Party { get; set; }
    public Data()
    {
        this.Party= new Party();
    }
}

public class Party
{
    string name;
    string postbox;

    public Tax Tax { get; set; }

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public string PostBox
    {
        get { return postbox; }
        set { this.postbox = value; }
    }
}

public class Tax
{
    string companyid;

    public string CompanyID
    {
        get { return companyid; }
        set { this.companyid = value; }
    }
}
  • 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-25T13:45:51+00:00Added an answer on May 25, 2026 at 1:45 pm

    You want:

    // Modifies the existing Party created in the Data constructor
    select new Data 
    { 
        Party =
        {
            Name = xElem.Element("Name").Value, 
            PostBox = xElem.Element("PostBox").Value
        }
    }
    

    or:

    // Creates a new Party and then calls the Data.Party setter
    select new Data 
    { 
        Party = new Party
        {
            Name = xElem.Element("Name").Value, 
            PostBox = xElem.Element("PostBox").Value
        }
    }
    

    Note that this has nothing to do with XML and nothing really to do with LINQ – it’s just using object initializer features.

    One thing you may want to consider is using the explicit conversion from XElement to string instead of using Value – that way if an element is missing, you get a null reference instead of an exception. It depends on what behaviour you want, but it’s worth knowing about as an option.

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

Sidebar

Related Questions

I want to select a specific range of elements within my XML files Here
I want to select data between 1 week ago data until today data: SELECT
I want to do following: Make Oracle stored procedure for read all data from
I am trying to replace 1234567890123456 with ************3456 with the following code: Regex.Replace(xml,@\b\d{13,16}\b, string.Concat(new
i have the following question: I have a XML file with some elements that
I actually want to achieve the following description This is the table argument I
I have following xml which same child element (Filed) and I want to get
I have the following XML file : <?xml version=1.0?> <Table69> </Table69> And want to
I have the following XML file with page nodes which I want to read
I have the following XML data in SQLServer 2008 R2 DECLARE @data XML SET

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.