使用替换组反序列化消息时出现问题

发布于 2024-11-15 06:31:50 字数 6512 浏览 3 评论 0原文

我有以下使用 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(message);

反序列化后,我得到一个空的数据库部分(即 Radars.radar[0] .数据库.长度== 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>

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

I have the following XML message that I deserialize with XmlSerializer.
For some reasons, deserialization of this message strips the Database section of the XML message.
I need some help in deserializing this message in its entirety.

This is a sample test XML message.

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

I am using XmlSerializer to deserialize the above message like this:

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

After deserializing it, I get an empty Database section (meaning radars.radar[0].Database.Length == 0).

The schema of the message is specified in a separate XSD file. I use xsd.exe to generate C# classes from it.
This is the partial content of the XSD file.

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

So how can I deserialize the message with Database section?

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

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

发布评论

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

评论(1

情绪少女 2024-11-22 06:31:50

尝试在代码中创建相同的对象结构,将其序列化并查看数据和序列化日期之间的差异,然后您就会知道出了什么问题。

Try create the same object structure in code, serialize it and see he differences between your data and serialized date, than you'll know whats wrong.

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