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;
}