我有一個類,它定義了一個請求xml 我想用linq來解析xml到類(稍後做相反的) 我得到那個請求是空的,並在函數返回該類的所有禮節「MidaClientID‘MidaClientID’這個名字在目前情況下不存在」解析XML到類使用LINQ
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> { };
}
問題是,我試圖向他們插入值的類請求的所有屬性不存在。這是我的主要problam – Danny
這個問題是在第二天,如果我能看到的屬性在類爲什麼我得到他們沒有定義,我不能設置屬性值。 – Danny
你可以改變你的類的所有實例以包含它的名字空間嗎?例。將列表更改爲列表 –
Pinoy2015