当我尝试使用XmlSerializer
类将XML文件反序列化为对象时出现问题。
我的XML文件如下所示:
<fx:FIBEX xmlns:fx="http://www.asam.net/xml/fbx" xmlns:ho="http://www.asam.net/xml" xmlns:ethernet="http://www.asam.net/xml/fbx/ethernet" xmlns:it="http://www.asam.net/xml/fbx/it" xmlns:service="http://www.asam.net/xml/fbx/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="" VERSION="4.1.0">
<fx:ELEMENTS>
<fx:CLUSTERS>
<fx:CLUSTER xsi:type="ethernet:CLUSTER-TYPE" ID="ID_CLUSTER_MAIN_1">
<ho:SHORT-NAME>SomeIpDatabase</ho:SHORT-NAME>
<fx:SPEED>1000000000</fx:SPEED>
<fx:IS-HIGH-LOW-BIT-ORDER>false</fx:IS-HIGH-LOW-BIT-ORDER>
<fx:BIT-COUNTING-POLICY>SAWTOOTH</fx:BIT-COUNTING-POLICY>
<fx:PROTOCOL>ETHERNET</fx:PROTOCOL>
<fx:PHYSICAL>OABR</fx:PHYSICAL>
<fx:CHANNEL-REFS>
<fx:CHANNEL-REF ID-REF="ID_CHANNEL_SOME_IP_1" />
</fx:CHANNEL-REFS>
<fx:MAX-FRAME-LENGTH>1500</fx:MAX-FRAME-LENGTH>
<ethernet:MAC-MULTICAST-GROUPS>
<ethernet:MAC-MULTICAST-GROUP ID="ID_CLUSTER_MAIN_1_ID_MACMULTICASTGROUP_SD_1">
<ho:SHORT-NAME>SD</ho:SHORT-NAME>
<ethernet:MAC-MULTICAST-ADDRESS>01:00:5E:40:FF:FB</ethernet:MAC-MULTICAST-ADDRESS>
</ethernet:MAC-MULTICAST-GROUP>
<ethernet:MAC-MULTICAST-GROUP ID="ID_CLUSTER_MAIN_1_ID_MACMULTICASTGROUP_BROADCAST_1">
<ho:SHORT-NAME>BROADCAST</ho:SHORT-NAME>
<ethernet:MAC-MULTICAST-ADDRESS>FF:FF:FF:FF:FF:FF</ethernet:MAC-MULTICAST-ADDRESS>
</ethernet:MAC-MULTICAST-GROUP>
</ethernet:MAC-MULTICAST-GROUPS>
</fx:CLUSTER>
<!--Additional CLUSTER elements omitted-->
</fx:CLUSTERS>
</fx:ELEMENTS>
<!--PROJECT elements omitted-->
</fx:FIBEX>
当我尝试反序列化XML文件时,我收到以下错误:
System.InvalidOperationException: Error in XML-Dokument (11,5). ---> System.InvalidOperationException: The specified type was not recognized: Name='CLUSTER-TYPE', Namespace='http://www.asam.net/xml/fbx/ethernet', at <CLUSTER xmlns='http://www.asam.net/xml/fbx'>.
我的反序列化器类看起来像这样:
public static T DeserializeXMLFileToObject<T>(string XmlFilename)
{
T returnObject = default(T);
if (string.IsNullOrEmpty(XmlFilename)) return default(T);
try
{
StreamReader xmlStream = new StreamReader(XmlFilename);
XmlSerializer serializer = new XmlSerializer(typeof(T));
returnObject = (T)serializer.Deserialize(xmlStream);
}
catch (Exception ex)
{
Console.Write("{1} Es ist ein Fehler aufgetreten {0}", ex, DateTime.Now);
}
return returnObject;
}
应该包含反序列化元素和XML文件属性的类如下所示:
[XmlRoot("FIBEX", Namespace = fxNameSpace)]
public class Fibextoobject
{
[XmlElement("PROJECT", Namespace = fxNameSpace)]
public Project project { get; set; }
[XmlElement("ELEMENTS", Namespace = fxNameSpace)]
public Elements elements { get; set; }
public class Project
{
[XmlAttribute("OID", Namespace = hoNameSpace)]
public string OID { get; set; }
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlElement("SHORT-NAME", Namespace = hoNameSpace)]
public string shortname { get; set; }
[XmlElement("LONG-NAME", Namespace = hoNameSpace)]
public string longname { get; set; }
}
public class Elements
{
[XmlArray("CLUSTERS", Namespace = fxNameSpace)]
[XmlArrayItem("CLUSTER", Namespace = fxNameSpace)]
public List<Cluster> cluster { get; set; }
}
public class Cluster
{
[XmlAttribute("ID")]
public string ID { get; set; }
[XmlElement("SHORT-NAME", Namespace = hoNameSpace)]
public string shortname { get; set; }
[XmlElement("SPEED", Namespace = fxNameSpace)]
public string speed { get; set; }
}
}
如何使用xsi:type属性成功反序列化XML文件,并在值xsi:type="ethernet:CLUSTER-TYPE"
中使用冒号和连字符?