2010-09-22 72 views
2

我想從XML中提取數據。我使用這個XML文件的XSD生成了C#類。我試圖從XML文件中獲取數據並將其保存爲C#類。 我遇到NULLReferenceException被用戶取消; 「你調用的對象是空的。」錯誤。未將對象引用設置爲對象的實例。 LINQ to XML errror

我的XML文件

<clue_personal_auto xmlns = "http://cp.com/rules/client"> 
<admin> 
<quoteback name="trackingNUmber">67890 </quoteback> 
</admin> 
<report> 
    <inquiry_processing_summary> 
    <classification> inqiury </classification> 
    </inquiry_processing_summary> 
    <results_dataset> 
     <claims_history type="Subject Section"> 
     <claim id = "s1" unit-number="1" sequence-number="1" number="11111"> 
     <scope_of_claim>Full Scope</scope_of_claim> 
     <claim_date>05/19/2005</claim_date> 
     <claim_age years="1" months="10"/> 
     <clue_file_number>222221</clue_file_number> 
     <policy number="XX111111"> 
      <issuer> CUSTOMER TEST </issuer> 
      <auto_type> Personal Auto</auto_type> 
      <fsi_issuer>Field absent on inquiry</fsi_issuer> 
     </policy> 
     <subject classification="POlicy HOlder"> 
     <name> 
     <first>SADIE</first> 
     <last>DOE</last> 
     <fsi_first>Discrepancy</fsi_first> 
     <fsi_middle>Field absent on inquiry and rport</fsi_middle> 
     <fsi_last>Match</fsi_last> 
     <fsi_suffix>Field absent on inquiry and report</fsi_suffix> 
     </name> 
     <ssn>123456789</ssn> 
     <driversLicense number="999999999" state="GA"> 
     <fsi_state>Field abscent on inquiry</fsi_state> 
     <fsi_number>Field absent on inquiry</fsi_number> 
     </driversLicense> 
     </subject> 
     <claimPayment amount_paid="2841.00" type="Collision" disposition="Open"/> 
     <claimPayment amount_paid="0.00" type="Physical/Property Damage" disposition="Open"/> 
     <claim_association_indicator>Indicates the Vehicle Operator was the person matched </claim_association_indicator> 
     <vehicle> 
     <model_year>1995</model_year> 
     <model_make>Nissan Sentra</model_make> 
     <vin> 9N9ABC99D434kjlkfajsdkl</vin> 
     <disposition>Not Reported </disposition> 
     <fsi_model_year>Field absent on inquiry and report</fsi_model_year> 
     <fsi_model_make>Field absent on inquiry and report</fsi_model_make> 
     <fsi_vin>Field absenton inquiry and report</fsi_vin> 
     </vehicle> 
     </claim> 
     <claim id = "s2" unit-number="2" sequence-number="2" number="22222"> 
     <scope_of_claim>Limited scope</scope_of_claim> 
     </claim> 
     <claim id = "s3" unit-number="3" sequence-number="3" number="33333"> 
     </claim> 
     </claims_history> 
     <inquiry_history name ="GO America" id="1" date="09/10/2010"/> 
    </results_dataset> 
</report> 

我的查詢:

List<results_dataset> resultdatasets = 
    (from b in query.Descendants(xmlns + "results_dataset") 
     select new results_dataset 
      { 
      claimsHistory = 
        (from c in query.Descendants(xmlns + "claims_history") 
         select new Claims_history 
         { 
          claim = (from d in query.Descendants(xmlns + "claim") 
           select new Claim() 
           { 
           policy = (from e in query.Descendants(xmlns + "policy") 
              **select new cluePolicyType() 
              { 
              issuer = e.Element("issuer").Value , 
              number = e.Attribute("number").Value , 
              auto_type = e.Element("auto_type").Value, 
              fsi_issuer= e.Element("fsi_issuer").Value, 
              } ** 
             ).ToList(), 
          scope_of_claim = d.Element("scope_of_claim").Value, 
          type = c.Attribute("type").Value 

          }).ToList() 
       } 
     ).ToList(); 

之間********是我得到這個error.If我刪除的區域在********的{}區域內的代碼,則無錯誤。我無法弄清楚。這讓我瘋狂。

+0

幾乎所有的'NullReferenceException'都是一樣的。請參閱「[什麼是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)」的一些提示。 – 2014-05-15 03:05:34

回答

2

所以,你已經減少了問題這些行:

issuer = e.Element("issuer").Value , 
number = e.Attribute("number").Value , 
auto_type = e.Element("auto_type").Value, 
fsi_issuer= e.Element("fsi_issuer").Value 

如果不能對代碼進行測試,我的猜測是,元素或屬性(發行人/數字/ auto_type/fsi_issuer)根本沒有按」你的某些元素是存在的。如果沒有找到指定的屬性,它將返回null,當您解析.Value時,它將拋出您的NRE。

相關問題