C# XML 反序列化器无法反序列化子节点

发布于 2024-09-05 18:39:45 字数 9945 浏览 4 评论 0原文

我有一个 XSD 和一个由 XSD.EXE 生成的类,我用它来反序列化 XML 文档。由于某种原因,XML 文档中相当深的节点无法反序列化,而且我找不到为什么会这样做...

以下是我反序列化的方式:

XmlSchema schema = XmlSchema.Read(XmlReader.Create("X12_00401_271.xsd"), null);
XmlSerializer _xSerializer = new XmlSerializer(typeof(X12_00401_271));
using (XmlReader _xReader = XmlReader.Create(path))
{
  _xResponse = _xSerializer.Deserialize(_xReader) as X12_00401_271;
}

XSD 的相关部分如下所示:

<xs:element name="TS271A1_2110C_Loop">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo structure="delimited" delimiter_type="inherit_record" 
    field_order="infix" count_ignore="yes" child_delimiter="default" 
    trigger_field="EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C/EB01__EligibilityOrBenefitInformation" 
    trigger_value="1 2 3 4 5 6 7 8 A B C D E F G H I J K L M N O P Q R S T U V W X Y MC CB" 
    notes="Subscriber Eligibility or Benefit Information" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="1" maxOccurs="1" ref="EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="HSD_HealthCareServicesDelivery_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="REF_SubscriberAdditionalIdentification_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="20" ref="DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="AAA_SubscriberRequestValidation_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="10" ref="MSG_MessageText_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="10" ref="TS271A1_2115C_Loop" />
    <xs:element minOccurs="0" name="LSLoop_2110C">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="1" maxOccurs="1" ref="LS_Header_TS271A1_2110C" />
          <xs:element minOccurs="0" maxOccurs="1" ref="TS271A1_2120C_Loop" />
          <xs:element minOccurs="1" maxOccurs="1" ref="LE_Trailer_TS271A1_2110C" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

其中“LS_Header_TS271A1_2110C”到“LE_Trailer_TS271A_2110C”无法反序列化

这三个片段是:

<xs:element name="LS_Header_TS271A1_2110C">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo tag_name="LS_Header_TS271A1_2110C" structure="delimited" 
     delimiter_type="inherit_field" field_order="prefix" count_ignore="no" 
     child_delimiter="default" notes="Loop Header" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element name="LS01__LoopIdentifierCode">
      <xs:annotation>
        <xs:appinfo>
          <b:fieldInfo edi_datatype="AN" justification="left">
          </b:fieldInfo>
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="X12_AN">
          <xs:minLength value="1" />
          <xs:maxLength value="6" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:element name="TS271A1_2120C_Loop">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo structure="delimited" delimiter_type="inherit_record" field_order="infix" 
     count_ignore="yes" child_delimiter="default" 
     trigger_field="NM1_SubscriberBenefitRelatedEntityName_TS271A1_2120C/NM101__EntityIdentifierCode" trigger_value="13 1P 2B 36 73 FA GP IL LR P3 P4 P5 PR SEP TTP VN X3 PRP" 
     notes="Subscriber Benefit Related Entity Name" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="1" maxOccurs="1" ref="NM1_SubscriberBenefitRelatedEntityName_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="N3_SubscriberBenefitRelatedEntityAddress_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="N4_SubscriberBenefitRelatedCityStateZIPCode_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="3" ref="PER_SubscriberBenefitRelatedEntityContactInformation_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="PRV_SubscriberBenefitRelatedProviderInformation_TS271A1_2120C" />
  </xs:sequence>
</xs:complexType>

<xs:element name="LE_Trailer_TS271A1_2110C">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo tag_name="LE_Trailer_TS271A1_2110C" structure="delimited" 
     delimiter_type="inherit_field" field_order="prefix" count_ignore="no" 
     child_delimiter="default" notes="Loop Trailer" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element name="LE01__LoopIdentifierCode">
      <xs:annotation>
        <xs:appinfo>
          <b:fieldInfo edi_datatype="AN" justification="left">
          </b:fieldInfo>
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="X12_AN">
          <xs:minLength value="1" />
          <xs:maxLength value="6" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

最后一个正确循环的生成类是:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]

[System.SerializedAttribute()] [系统.诊断.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“代码”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,命名空间=“http://schemas.microsoft.com/BizTalk/EDI/X12/2006”)] [System.Xml.Serialization.XmlRootAttribute(Namespace =“http://schemas.microsoft.com/BizTalk/EDI/X12/2006”,IsNullable = false)] public parts class TS271A1_2110C_Loop {

private EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField;

private HSD_HealthCareServicesDelivery_TS271A1_2110C[] hSD_HealthCareServicesDelivery_TS271A1_2110CField;

private REF_SubscriberAdditionalIdentification_TS271A1_2110C[] rEF_SubscriberAdditionalIdentification_TS271A1_2110CField;

private DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C[] dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField;

private AAA_SubscriberRequestValidation_TS271A1_2110C[] aAA_SubscriberRequestValidation_TS271A1_2110CField;

private MSG_MessageText_TS271A1_2110C[] mSG_MessageText_TS271A1_2110CField;

private TS271A1_2115C_Loop[] tS271A1_2115C_LoopField;

private TS271A1_2110C_LoopLSLoop lSLoopField;

/// <remarks/>
public EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C {
    get {
        return this.eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField;
    }
    set {
        this.eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("HSD_HealthCareServicesDelivery_TS271A1_2110C")]
public HSD_HealthCareServicesDelivery_TS271A1_2110C[] HSD_HealthCareServicesDelivery_TS271A1_2110C {
    get {
        return this.hSD_HealthCareServicesDelivery_TS271A1_2110CField;
    }
    set {
        this.hSD_HealthCareServicesDelivery_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("REF_SubscriberAdditionalIdentification_TS271A1_2110C")]
public REF_SubscriberAdditionalIdentification_TS271A1_2110C[] REF_SubscriberAdditionalIdentification_TS271A1_2110C {
    get {
        return this.rEF_SubscriberAdditionalIdentification_TS271A1_2110CField;
    }
    set {
        this.rEF_SubscriberAdditionalIdentification_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C")]
public DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C[] DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C {
    get {
        return this.dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField;
    }
    set {
        this.dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("AAA_SubscriberRequestValidation_TS271A1_2110C")]
public AAA_SubscriberRequestValidation_TS271A1_2110C[] AAA_SubscriberRequestValidation_TS271A1_2110C {
    get {
        return this.aAA_SubscriberRequestValidation_TS271A1_2110CField;
    }
    set {
        this.aAA_SubscriberRequestValidation_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MSG_MessageText_TS271A1_2110C")]
public MSG_MessageText_TS271A1_2110C[] MSG_MessageText_TS271A1_2110C {
    get {
        return this.mSG_MessageText_TS271A1_2110CField;
    }
    set {
        this.mSG_MessageText_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("TS271A1_2115C_Loop")]
public TS271A1_2115C_Loop[] TS271A1_2115C_Loop {
    get {
        return this.tS271A1_2115C_LoopField;
    }
    set {
        this.tS271A1_2115C_LoopField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TS271A1_2110C_LoopLSLoop LSLoop {
    get {
        return this.lSLoopField;
    }
    set {
        this.lSLoopField = value;
    }
}

}

对于那些熟悉该标准的人来说,这具体是一个 HIPAA 271,我无法反序列化 2120C 循环。对于其他人来说,我认为这对具体问题并不重要...

我已经验证了我需要的节点同时存在于 XSD 和我正在解析的实际 XML 文件中。关于下一步去哪里有什么建议吗?

I have an XSD and a class generated by XSD.EXE I'm using to deserialize an XML document. For some reason, a node fairly deep into the XML document is failing to deserialize, and I can't find why it would be doing so...

Here's how I'm deserializing:

XmlSchema schema = XmlSchema.Read(XmlReader.Create("X12_00401_271.xsd"), null);
XmlSerializer _xSerializer = new XmlSerializer(typeof(X12_00401_271));
using (XmlReader _xReader = XmlReader.Create(path))
{
  _xResponse = _xSerializer.Deserialize(_xReader) as X12_00401_271;
}

The relevant part of the XSD looks something like:

<xs:element name="TS271A1_2110C_Loop">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo structure="delimited" delimiter_type="inherit_record" 
    field_order="infix" count_ignore="yes" child_delimiter="default" 
    trigger_field="EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C/EB01__EligibilityOrBenefitInformation" 
    trigger_value="1 2 3 4 5 6 7 8 A B C D E F G H I J K L M N O P Q R S T U V W X Y MC CB" 
    notes="Subscriber Eligibility or Benefit Information" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="1" maxOccurs="1" ref="EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="HSD_HealthCareServicesDelivery_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="REF_SubscriberAdditionalIdentification_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="20" ref="DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="9" ref="AAA_SubscriberRequestValidation_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="10" ref="MSG_MessageText_TS271A1_2110C" />
    <xs:element minOccurs="0" maxOccurs="10" ref="TS271A1_2115C_Loop" />
    <xs:element minOccurs="0" name="LSLoop_2110C">
      <xs:complexType>
        <xs:sequence>
          <xs:element minOccurs="1" maxOccurs="1" ref="LS_Header_TS271A1_2110C" />
          <xs:element minOccurs="0" maxOccurs="1" ref="TS271A1_2120C_Loop" />
          <xs:element minOccurs="1" maxOccurs="1" ref="LE_Trailer_TS271A1_2110C" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

Where 'LS_Header_TS271A1_2110C' thru 'LE_Trailer_TS271A_2110C' are failing to deserialize

Those three snippets are:

<xs:element name="LS_Header_TS271A1_2110C">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo tag_name="LS_Header_TS271A1_2110C" structure="delimited" 
     delimiter_type="inherit_field" field_order="prefix" count_ignore="no" 
     child_delimiter="default" notes="Loop Header" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element name="LS01__LoopIdentifierCode">
      <xs:annotation>
        <xs:appinfo>
          <b:fieldInfo edi_datatype="AN" justification="left">
          </b:fieldInfo>
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="X12_AN">
          <xs:minLength value="1" />
          <xs:maxLength value="6" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

<xs:element name="TS271A1_2120C_Loop">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo structure="delimited" delimiter_type="inherit_record" field_order="infix" 
     count_ignore="yes" child_delimiter="default" 
     trigger_field="NM1_SubscriberBenefitRelatedEntityName_TS271A1_2120C/NM101__EntityIdentifierCode" trigger_value="13 1P 2B 36 73 FA GP IL LR P3 P4 P5 PR SEP TTP VN X3 PRP" 
     notes="Subscriber Benefit Related Entity Name" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element minOccurs="1" maxOccurs="1" ref="NM1_SubscriberBenefitRelatedEntityName_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="N3_SubscriberBenefitRelatedEntityAddress_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="N4_SubscriberBenefitRelatedCityStateZIPCode_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="3" ref="PER_SubscriberBenefitRelatedEntityContactInformation_TS271A1_2120C" />
    <xs:element minOccurs="0" maxOccurs="1" ref="PRV_SubscriberBenefitRelatedProviderInformation_TS271A1_2120C" />
  </xs:sequence>
</xs:complexType>

<xs:element name="LE_Trailer_TS271A1_2110C">
<xs:annotation>
  <xs:appinfo>
    <b:recordInfo tag_name="LE_Trailer_TS271A1_2110C" structure="delimited" 
     delimiter_type="inherit_field" field_order="prefix" count_ignore="no" 
     child_delimiter="default" notes="Loop Trailer" />
  </xs:appinfo>
</xs:annotation>
<xs:complexType>
  <xs:sequence>
    <xs:element name="LE01__LoopIdentifierCode">
      <xs:annotation>
        <xs:appinfo>
          <b:fieldInfo edi_datatype="AN" justification="left">
          </b:fieldInfo>
        </xs:appinfo>
      </xs:annotation>
      <xs:simpleType>
        <xs:restriction base="X12_AN">
          <xs:minLength value="1" />
          <xs:maxLength value="6" />
        </xs:restriction>
      </xs:simpleType>
    </xs:element>
  </xs:sequence>
</xs:complexType>

The Generated Class for the last correct loop is:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://schemas.microsoft.com/BizTalk/EDI/X12/2006")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://schemas.microsoft.com/BizTalk/EDI/X12/2006", IsNullable=false)]
public partial class TS271A1_2110C_Loop {

private EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField;

private HSD_HealthCareServicesDelivery_TS271A1_2110C[] hSD_HealthCareServicesDelivery_TS271A1_2110CField;

private REF_SubscriberAdditionalIdentification_TS271A1_2110C[] rEF_SubscriberAdditionalIdentification_TS271A1_2110CField;

private DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C[] dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField;

private AAA_SubscriberRequestValidation_TS271A1_2110C[] aAA_SubscriberRequestValidation_TS271A1_2110CField;

private MSG_MessageText_TS271A1_2110C[] mSG_MessageText_TS271A1_2110CField;

private TS271A1_2115C_Loop[] tS271A1_2115C_LoopField;

private TS271A1_2110C_LoopLSLoop lSLoopField;

/// <remarks/>
public EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C EB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110C {
    get {
        return this.eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField;
    }
    set {
        this.eB_SubscriberEligibilityOrBenefitInformation_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("HSD_HealthCareServicesDelivery_TS271A1_2110C")]
public HSD_HealthCareServicesDelivery_TS271A1_2110C[] HSD_HealthCareServicesDelivery_TS271A1_2110C {
    get {
        return this.hSD_HealthCareServicesDelivery_TS271A1_2110CField;
    }
    set {
        this.hSD_HealthCareServicesDelivery_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("REF_SubscriberAdditionalIdentification_TS271A1_2110C")]
public REF_SubscriberAdditionalIdentification_TS271A1_2110C[] REF_SubscriberAdditionalIdentification_TS271A1_2110C {
    get {
        return this.rEF_SubscriberAdditionalIdentification_TS271A1_2110CField;
    }
    set {
        this.rEF_SubscriberAdditionalIdentification_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C")]
public DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C[] DTP_SubscriberEligibilityBenefitDate_TS271A1_2110C {
    get {
        return this.dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField;
    }
    set {
        this.dTP_SubscriberEligibilityBenefitDate_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("AAA_SubscriberRequestValidation_TS271A1_2110C")]
public AAA_SubscriberRequestValidation_TS271A1_2110C[] AAA_SubscriberRequestValidation_TS271A1_2110C {
    get {
        return this.aAA_SubscriberRequestValidation_TS271A1_2110CField;
    }
    set {
        this.aAA_SubscriberRequestValidation_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("MSG_MessageText_TS271A1_2110C")]
public MSG_MessageText_TS271A1_2110C[] MSG_MessageText_TS271A1_2110C {
    get {
        return this.mSG_MessageText_TS271A1_2110CField;
    }
    set {
        this.mSG_MessageText_TS271A1_2110CField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("TS271A1_2115C_Loop")]
public TS271A1_2115C_Loop[] TS271A1_2115C_Loop {
    get {
        return this.tS271A1_2115C_LoopField;
    }
    set {
        this.tS271A1_2115C_LoopField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public TS271A1_2110C_LoopLSLoop LSLoop {
    get {
        return this.lSLoopField;
    }
    set {
        this.lSLoopField = value;
    }
}

}

For those familiar with the standard, this is specifically a HIPAA 271 and I'm failing to deserialize the 2120C loop. For the rest of you, I don't think that matters to the specific problem...

I've verified the node I need exists both in the XSD and the actual XML file I'm parsing. Any suggestions on where to look next?

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

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

发布评论

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

评论(1

差↓一点笑了 2024-09-12 18:39:45

我也遇到过这个。据我所知,XmlSerializer 不支持子节点上的命名空间。您必须显式地将它们的名称空间设置为“”(使用 xmlns="" 或类似的)。

I've run into this too. From what I can see, the XmlSerializer does not support namespaces on child nodes. You have to explicitly set their namespace to "" (using xmlns="" or suchlike).

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