使用substitutionGroup反序列化消息时出现问题

本文关键字:问题 消息 substitutionGroup 反序列化 使用 | 更新日期: 2023-09-27 17:59:22

我使用XmlSerializer反序列化了以下XML消息。由于某些原因,此消息的反序列化会剥离XML消息的数据库部分。我需要一些帮助来反序列化整个消息。

这是一个测试XML消息示例。

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

我正在使用XmlSerializer来反序列化上面的消息,如下所示:

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

在对它进行反序列化之后,我得到了一个空的Database部分(意思是raders.rar[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>

那么,如何使用数据库部分反序列化消息呢?

使用substitutionGroup反序列化消息时出现问题

试着在代码中创建相同的对象结构,序列化它,看看数据和序列化日期之间的差异,这样你就会知道哪里出了问题。