你调用的对象是空的。 LINQ 到 XML 错误

发布于 2024-09-24 13:25:00 字数 4674 浏览 3 评论 0原文

我正在尝试从 XML 中提取数据。我使用此 XML 文件的 XSD 生成了 C# 类。我正在尝试从 XML 文件获取数据并将其保存为 C# 类。 我遇到了 NULLReferenceException Was Unhadled by User; “你调用的对象是空的。”错误。

我的 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();

******** 之间的区域是我收到此错误的地方。如果我删除 ******** 的 {} 区域内的代码,则不会出现错误。我无法弄清楚。这让我发疯。

I am trying to extract data from XML. I generated C# classes using XSD of this XML file. I am trying to get the data from XML file and save it C# class.
I am running into NULLReferenceException Was Unhadled by User; "Object reference not set to an instance of an object." error.

My XML File

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

My query :

  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();

The area in between ******** is where I get this error.If I remove the code which is inside the {} area of ******** then no error. I am not able to figure it out. it is driving me crazy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2024-10-01 13:25:00

因此,您已将问题简化为以下几行:

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

在无法测试您的代码的情况下,我的猜测是您的某些元素根本不存在元素或属性(issuer/number/auto_type/fsi_issuer)。如果未找到指定的属性,它将返回 null,这将在解析 .Value 时抛出 NRE。

So you've reduced the problem to these lines:

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

Without being able to test your code, my guess is that the Element or Attribute (issuer/number/auto_type/fsi_issuer) simply doesn't exist for some of your elements. If a specified attribute is not found, it will return null, which will throw your NRE when you resolve .Value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文