2010-06-14 148 views
0

我有一個XSD和一個由XSD.EXE生成的類我用於反序列化一個XML文檔。出於某種原因,相當深入到XML文檔中的節點失敗反序列化,我找不到爲什麼會這樣......C#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.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute( 「代碼」)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType =真,命名空間=「http://schemas.microsoft.com/BizTalk/EDI/X12/2006」)] [System.Xml.Serialization.XmlRootAttribute(Namespace =「http://schemas.microsoft.com/BizTalk/EDI/X12/2006" ,ISNULLABLE =假)] 公共部分類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文件中存在的節點。任何關於下一步看什麼的建議?

+0

當你說它無法反序列化時,你是否得到某種異常,或者當反序列化完成時對象只是NULL?另外,源XML是如何生成的? – Toby 2010-06-14 15:12:38

+0

您可以將鏈接(或)文本粘貼到您的xsd和生成的ChildNode4類(最後一個節點正確反序列化)嗎? – Grzenio 2010-06-14 15:13:51

+0

@Toby:反序列化完成時,該對象爲空。我使用XSD.EXE @Grzenio:這是一個很大的XSD,我會嘗試添加相關的片段。 – AllenG 2010-06-14 16:01:40

回答

1

我也遇到過這個。從我所看到的,XmlSerializer不支持子節點上的名稱空間。你必須明確地將他們的命名空間設置爲「」(使用xmlns =「」或諸如此類)。

+0

這是最好的答案,如果你確切地指定了你的意思,它會有所幫助,但它的工作,謝謝老兄。 – user1494173 2017-07-20 20:44:14