2012-06-25 99 views
2

我有一個類,它定義了一個請求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> { }; 
     } 

回答

0

爲什麼你越來越空的原因是因爲你是返回列表的新實例。

return new List<Request> { }; 

可以更換的代碼上面的行至

return req; 

如以下代碼所示。

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

}

+0

問題是,我試圖向他們插入值的類請求的所有屬性不存在。這是我的主要problam – Danny

+0

這個問題是在第二天,如果我能看到的屬性在類爲什麼我得到他們沒有定義,我不能設置屬性值。 – Danny

+0

你可以改變你的類的所有實例以包含它的名字空間嗎?例。將列表更改爲列表 Pinoy2015

0

您可以通過序列化對象做到這一點。 「序列化」是指以某種方式獲取對象的狀態並持久化它的過程,Microsoft .NET Framework包含可將任何對象序列化爲XML的強大對象,System.Xml.Serialization命名空間提供了此功能。 「

http://support.microsoft.com/kb/815813

+0

謝謝你的迴應,這將是我的第二部分非常好,我試圖像的例子,它工作正常。 – Danny