2011-06-15 3 views
0

다음 XML 메시지가 XmlSerializer로 역 직렬화되어 있습니다. 몇 가지 이유로이 메시지를 deserialize하면 XML 메시지의 Database 섹션이 제거됩니다. 이 메시지 전체를 역 직렬화하는 데 도움이 필요합니다.substitutionGroup을 사용하여 메시지를 deserialize 할 때의 문제점

이것은 샘플 테스트 XML 메시지입니다. 나는이 같은 위의 메시지를 역 직렬화 할 XmlSerializer를 사용하고

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<Radars xmlns="http://mycompany.com/2008/c2s2"> 
    <Radar> 
    <Identification> 
     <Identifier>140:141</Identifier> 
     <IP>10.216.1.79</IP> 
     <Label>Radar 1</Label> 
     <Model>Radar123</Model> 
     <Category>Civil</Category> 
    </Identification> 
    <Database> 
     <Radar456General> 
     <Identifier>203</Identifier> 
     <Version>8</Version> 
     <RadarStartStop>false</RadarStartStop> 
     <AntennaRotationSpeed>false</AntennaRotationSpeed> 
     <RadarAntennaRotation>false</RadarAntennaRotation> 
     <AntennaStaringPosition>0</AntennaStaringPosition> 
     </Radar456General> 
    </Database> 
    </Radar> 
</Radars> 

:

public static T Deserialize<T>(string message) 
    { 
     object obj = null; 

     try 
     { 
      XmlSerializer xs = new XmlSerializer(typeof(T)); 
      StringReader reader = new StringReader(message); 
      XmlTextReader xmlReader = new XmlTextReader(reader); 

      obj = xs.Deserialize(xmlReader); 

      xmlReader.Close(); 
      reader.Close(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 
      throw ex; 
     } 

     return (T)obj; 
    } 

Radars radars = Serializer.Deserialize<Radars>(message);

를 직렬화 복원 후, 나는 얻을 빈 데이터베이스 섹션 (의미 radars.radar [0]. Database.Length == 0).

메시지 스키마는 별도의 XSD 파일에 지정됩니다. xsd.exe를 사용하여 C# 클래스를 생성합니다. 이것은 XSD 파일의 부분 내용입니다.

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      xmlns="http://mycompany.com/2008/c2s2" 
      targetNamespace="http://mycompany.com/2008/c2s2" 
      elementFormDefault="qualified" version="1.0"> 

    <xs:include schemaLocation="asterix_cat034.xsd"/> 
    <xs:include schemaLocation="asterix_cat253.xsd"/> 

    <xs:element name="Radars"> 
    <xs:annotation> 
     <xs:documentation>List of radar information</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="Radar" type="RadarType" minOccurs="0" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 

    <xs:complexType name="RadarType"> 
    <xs:sequence> 
     <xs:element name="Identification" type="RadarIdentificationType"/> 
     <xs:element name="Database" type="DatabaseType"/> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="Radar" type="RadarType"/> 

    <xs:complexType name="RadarIdentificationType"> 
    <xs:annotation> 
     <xs:documentation> 
     This type represents the radar identification. 
     </xs:documentation> 
    </xs:annotation> 
    <xs:sequence> 
     <xs:element name="Identifier" type="xs:string"/> 
     <xs:element name="IP" type="xs:string"/> 
     <xs:element name="Label" type="xs:string"/> 
     <xs:element name="Model" type="RadarModelType"/> 
     <xs:element name="Category" type="RadarCategoryType"/> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:simpleType name="RadarModelType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Radar123"/> 
     <xs:enumeration value="Radar456"/> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:simpleType name="RadarCategoryType"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Civil"/> 
     <xs:enumeration value="Military"/> 
    </xs:restriction> 
    </xs:simpleType> 

    <xs:complexType name="DatabaseSectionType"> 
    <xs:sequence> 
     <xs:element name="Identifier" type="xs:unsignedByte"/> 
     <xs:element name="Version" type="I253_100_DB_VER"/> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="DatabaseSectionType" type="DatabaseSectionType"> 
    <xs:annotation> 
     <xs:documentation>root of the substitution group</xs:documentation> 
    </xs:annotation> 
    </xs:element> 

    <xs:complexType name="DatabaseType"> 
    <xs:sequence> 
     <xs:element ref="DatabaseSectionType" minOccurs="1" maxOccurs="unbounded"/> 
    </xs:sequence> 
    </xs:complexType> 

    <xs:element name="Radar456General" substitutionGroup="DatabaseSectionType"> 
    <xs:annotation> 
     <xs:documentation>Radar456General database section</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:complexContent> 
     <xs:extension base="DatabaseSectionType"> 
      <xs:sequence> 
      <xs:element name="RadarStartStop" type="I253_100_Start_Stop"/> 
      <xs:element name="AntennaRotationSpeed" type="I253_100_ARS"/> 
      <xs:element name="RadarAntennaRotation" type="I253_100_ROT"/> 
      <xs:element name="AntennaStaringPosition" type="I253_100_ASP"/> 
      </xs:sequence> 
     </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
    </xs:element> 

    <xs:element name="Radar123General" substitutionGroup="DatabaseSectionType"> 
    <xs:annotation> 
     <xs:documentation>Radar123General database section</xs:documentation> 
    </xs:annotation> 
    <xs:complexType> 
     <xs:complexContent> 
     <xs:extension base="DatabaseSectionType"> 
      <xs:sequence> 
      <xs:element name="Shutdown" type="I253_100_Shutdown"/> 
      <xs:element name="RotationTransmission" type="I253_100_ROT_TX"/> 
      <xs:element name="SurfaceInstrumentedRange" type="I253_100_SIR"/> 
      <xs:element name="TransmitterTuneMode" type="I253_100_TT_mode"/> 
      </xs:sequence> 
     </xs:extension> 
     </xs:complexContent> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

그럼 어떻게 데이터베이스 섹션으로 메시지를 역 직렬화 할 수 있습니까?

답변

0

코드에서 동일한 개체 구조를 만들고 직렬화 한 다음 데이터와 serialize 된 날짜의 차이점을 확인하십시오.