XML反序列化:元素名称相同,但类型不同

本文关键字:类型 反序列化 元素 XML | 更新日期: 2023-09-27 18:20:00

我有两种不同类型的xml文件,需要使用单个类进行序列化。在序列化之前,我无法使用xml解析器。这是MSMQ的典型反序列化场景。

问题在于提供的xml中的message_string节点。

<?xml version="1.0" encoding="UTF-8"?>
<tms_msg>
 <transaction>
  <message_string>
     <latitude>latitidue valeu</latitude>
     <longitude>longitude</longitude>
  </message_string>
 </transaction>
</tms_msg>

和2型

<tms_msg>
 <transaction>
  <message_string>message string</message_string>
 </transaction>
</tms_msg>

用于反序列化的类是

public class transaction
{
    [XmlElement("message_string", typeof(Complextype))]
    public object[] StringsAndInts;
    [XmlElement("message_string", typeof(string))]
    public string stringValue;
}
[XmlRoot("tms_msg")]
public class tms_msg
{
    [XmlElement("transaction")]
    public transaction transaction;
}
public class Complextype
{
    public string latitude;
    public string longitude;
}

实施部分

 public class Program
{
    public Object CreateObject(string XMLString, Object YourClassObject)
    {
        XmlSerializer oXmlSerializer = new XmlSerializer(YourClassObject.GetType());
        //The StringReader will be the stream holder for the existing XML file
        YourClassObject = oXmlSerializer.Deserialize(new StringReader(XMLString));
        //initially deserialized, the data is represented by an object without a defined type
        return YourClassObject;
    }
    static void Main(string[] args)
    {
        tms_msg objempq = new tms_msg();
        objempq = (tms_msg)CreateObject(txtXML.Text, objempq);
    }
}

请不要提供xml解析和查找元素类型的建议。

XML反序列化:元素名称相同,但类型不同

[XmlRoot("tms_msg")]
    public class TmsMessage
    {
        [XmlElement("transaction")]
        public Transaction Transaction;
    }
    public class Transaction
    {
        [XmlElement("message_string", typeof(ComplexType))]
        public ComplexType[] ComplexObjects { get; set; }
    }
    public class ComplexType
    {
        [XmlElement("latitue")]
        public string Latitude { get; set; }
        [XmlElement("longitude")]
        public string Longitude { get; set; }
        [XmlText]
        public String Text { get; set; }
        //in other part of source code of this class, check the object type.
        //if it's type1 then property Text gets null; if it's type 2, property 
        //Latitude and Longitude get null.
    }

Text属性读取message string

    [Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class tms_msg
    {
        [System.Xml.Serialization.XmlElementAttribute("transaction")]
        public tms_msgTransaction[] transaction { get; set; }
    }
    [Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public class tms_msgTransaction
    {
        public tms_msgTransactionMessage_string message_string { get; set; }
    }
    [Serializable]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    public class tms_msgTransactionMessage_string
    {
        public string latitude { get; set; }
        public string longitude { get; set; }
        [System.Xml.Serialization.XmlTextAttribute()]
        public string[] Text { get; set; }
    }