我有一个定义请求 xml 的类我想使用 linq 将 xml 解析为类(稍后做相反的事情)我得到 req 为空,并且在返回类所有属性的函数中“MidaClientID 名称“MidaClientID”在当前上下文中不存在“
public class Request
{
public int MidaClientID { get; set; }
public int Password { get; set; }
public int CandNum { get; set; }
public string CandFirstName { get; set; }
public string CandLastName { get; set; }
public int CandPhone1 { get; set; }
public int CandPhone2 { get; set; }
public int CandPhone3 { get; set; }
public string CandAttach1 { get; set; }
public string CandAttach2 { get; set; }
public string CandAttach3 { get; set; }
public int ClientNum { get; set; }
public string ClientName { get; set; }
public int ContactNum { get; set; }
public string ContactFirstName { get; set; }
public string ContactLastName { get; set; }
public string ContactEmail { get; set; }
public int EventNum { get; set; }
public DateTime EventDate { get; set; }
public DateTime EventTime { get; set; }
public int QuestNum { get; set; }
}
[WebMethod]
public void GetXml()
{
XDocument doc = XDocument.Load(@".xml");
List<Request> req = CreateRequest(doc);
}
public static List<Request> CreateRequest(XDocument input)
{
IEnumerable<Request> req = from r in input.Descendants("Request")
select new Request()
{
//MidaClientID = (int)r.Attribute("MidaClientID"),
MidaClientID = Convert.ToInt32(r.Element("MidaClientID").Value),
Password = (int)r.Attribute("MidaClientPassword"),
CandNum = (int)r.Attribute("CandNum"),
CandFirstName = (string)r.Attribute("CandFirstName"),
CandLastName = (string)r.Attribute("CandLastName"),
CandPhone1 = (int)r.Attribute("CandPhone1"),
CandPhone2 = (int)r.Attribute("CandPhone2"),
CandPhone3 = (int)r.Attribute("CandPhone3"),
CandAttach1 = (string)r.Attribute("CandAttach1"),
CandAttach2 = (string)r.Attribute("CandAttach2"),
CandAttach3 = (string)r.Attribute("CandAttach3"),
ClientNum = (int)r.Attribute("ClientNum"),
ClientName = (string)r.Attribute("ClientName"),
ContactNum = (int)r.Attribute("ContactNum"),
ContactFirstName = (string)r.Attribute("ContactFirstName"),
ContactLastName = (string)r.Attribute("ContactLastName"),
ContactEmail = (string)r.Attribute("ContactEmail"),
EventNum = (int)r.Attribute("EventNum"),
EventDate = (DateTime)r.Attribute("EventDate"),
EventTime = (DateTime)r.Attribute("EventTime"),
QuestNum = (int)r.Attribute("QuestNum")
};
return new List<Request> { };
}