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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:18:42+00:00 2026-06-07T22:18:42+00:00

I have a class that defines the xml and another class that defines the

  • 0

I have a class that defines the xml and another class that defines the candidate and another class that it is a list of the class candidate.

I am trying to create dynamically the list according to number of nodes that I have in the xml.

I have tried all kind of variations and nothing worked, until the nodes Candidates I got all the information but the CandidateList came null all the time.

public class Candidate
{
}

public class CandidateList : List<Candidate>
{
}

public class Request
{
    public CandidateList CandidateList { get; set; }
}
<?xml version="1.0" encoding="utf-8"?>
<Requests>
  <Request>
    <MidaClientID>1040</MidaClientID>
    <!--elided other elements-->
    <OrderDescription></OrderDescription>
    <Candidates>
      <Candidate>
        <QuestNum>6</QuestNum>
        <!--elided other elements-->
        <EventNum>012</EventNum>
      </Candidate>
      <Candidate>
        <QuestNum>6</QuestNum>
         <!--elided other elements-->
        <EventNum>012</EventNum>
      </Candidate>
    </Candidates>
  </Request>
</Requests>
try
{
    IEnumerable<Request> req = from r in input.Descendants("Request")
    select new Request()
    {
        MidaClientID = (int)r.Element("MidaClientID") != 0 ? (int)r.Element("MidaClientID") : 0,
        Password = (string)r.Element("MidaClientPassword") != null ? (string)r.Element("MidaClientPassword") : string.Empty,
        ClientNum = (int)r.Element("ClientNum") != 0 ? (int)r.Element("ClientNum") : 0,
        ClientName = (string)r.Element("ClientName") != null ? (string)r.Element("ClientName") : string.Empty,
        ContactNum = (int)r.Element("ContactNum") != 0 ? (int)r.Element("ContactNum") : 0,
        ContactFirstName = (string)r.Element("ContactFirstName") != null ? (string)r.Element("ContactFirstName") : string.Empty,
        ContactLastName = (string)r.Element("ContactLastName") != null ? (string)r.Element("ContactLastName") : string.Empty,
        ContactEmail = (string)r.Element("ContactEmail") != null ? (string)r.Element("ContactEmail") : "",
        OrderID = (int)r.Element("OrderID") != 0 ? (int)r.Element("OrderID") : 0,
        OrderDesc = (string)r.Element("OrderDescription") != null ? (string)r.Element("OrderDescription") : "",
        CandidateList = (from i in input.Root.Element("Candidates").Elements("Candidate")
            select new Candidate()
            {
                QuestNum = (int)r.Element("QuestNum") != 0 ? (int)r.Element("QuestNum") : 0,
                CandNum = (int)r.Element("CandNum") != 0 ? (int)r.Element("CandNum") : 0,
                EventNum = (int)r.Element("EventNum") != 0 ? (int)r.Element("EventNum") : 0,
                EventDate = (string)(r.Element("EventDate")) == string.Empty ?
                            DateTime.Today : (DateTime)(r.Element("EventDate")),
                EventTime = (string)(r.Element("EventTime")) == string.Empty ?
                            DateTime.Now : (DateTime)(r.Element("EventTime")),
                CandFirstName = (string)r.Element("CandFirstName") != null ? (string)r.Element("CandFirstName") : string.Empty,
                CandLastName = (string)r.Element("CandLastName") != null ? (string)r.Element("CandLastName") : string.Empty,
                CandPhone1 = (string)r.Element("CandPhone1") != null ? (string)r.Element("CandPhone1") : string.Empty,
                CandPhone2 = (string)r.Element("CandPhone2") != null ? (string)r.Element("CandPhone2") : string.Empty,
                CandPhone3 = (string)r.Element("CandPhone3") != null ? (string)r.Element("CandPhone3") : string.Empty,
                CandAttach1 = (string)r.Element("CandAttach1") != null ? (string)r.Element("CandAttach1") : string.Empty,
                CandAttach2 = (string)r.Element("CandAttach2") != null ? (string)r.Element("CandAttach2") : string.Empty,
                CandAttach3 = (string)r.Element("CandAttach3") != null ? (string)r.Element("CandAttach3") : string.Empty
            }) as CandidateList
    };
    return req.ToList();
}

catch (Exception ex)
{
    throw ex;
}
  • 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-07T22:18:45+00:00Added an answer on June 7, 2026 at 10:18 pm

    Thanks for tidying up the question – having to scroll horizontally is a bit of a nightmare (will edit to show).

    Anyway – it seems that your primary problem is the bit where you select your Candidates node:

    CandidateList = (from i in input.Root.Element("Candidates").Elements("Candidate") 
    

    The problem being that the linq query does not produce a CandidateList instance. One way to do that will be to add a range constructor to your CandidateList type and feed the linq query to it. Also – I think the input.Root is wrong.

    public class CandidateList : List<Candidate>{
      public CandidateList() {}
      public CandidateList(IEnumerable<Candidate> range) : base(range) {}
    }
    
    //.. in 'new Request{' block
    select new Candidate {
      // ... other members elided
      CandidateList = new CandidateList(
        (from i in r.Element("Candidates").Elements("Candidate") 
        select new Candidate()     
        {   
          //...all the Candidate members
        })
      ) //<-- end of the constructor call
    }
    

    You’ll also need to drop the as CandidateList from the end of that query after moving it into the constructor, otherwise you’ll either get an empty CandidateList or, possibly an ArgumentNullException from the List<T> base class of CandidateList. Notice that I’ve change the from; since you want the Candidates/Candidate nodes for the current Requests/Request, which is given by r.

    Finally – all your property assignments to the Candidate object are coming from r – you need to change those to i (the product of the inner Linq statement).

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

Sidebar

Related Questions

I have a class that defines my level. I have an XML file in
I have a class that defines its own enum like this: public class Test
I have two different projects and in one, I have a class that defines
I have a Literal class that defines a text value and an ID. I
Assume that I have a particular class of object that defines a class method
I have an AbstractEntity class as superclass for all my entites that defines an
I have a Rails engine that defines a class method top(count) on a model
I have a class Bar that has a user-defined list of config keys and
I'm trying to make a base class that defines the interfaces for all derived
I have a class that extends view , that defines a custom drawing (a

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.