我有以下XML消息,我使用XmlSerializer反序列化。 出于某些原因,此消息的反序列化会删除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);
反序列化之后,我得到一个空数据库部分(意思是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>
那么如何使用Database部分反序列化消息?
答案 0 :(得分:0)
尝试在代码中创建相同的对象结构,对其进行序列化并查看数据和序列化日期之间的差异,而不是您知道什么是错误的。