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;
}
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:
The problem being that the linq query does not produce a
CandidateListinstance. One way to do that will be to add a range constructor to yourCandidateListtype and feed the linq query to it. Also – I think theinput.Rootis wrong.You’ll also need to drop the
as CandidateListfrom the end of that query after moving it into the constructor, otherwise you’ll either get an empty CandidateList or, possibly anArgumentNullExceptionfrom theList<T>base class ofCandidateList. Notice that I’ve change thefrom; since you want theCandidates/Candidatenodes for the currentRequests/Request, which is given byr.Finally – all your property assignments to the
Candidateobject are coming fromr– you need to change those toi(the product of the inner Linq statement).