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

时间:2015-10-30 02:40:58

标签: c# xml serialization deserialization xml-deserialization

我有2种不同类型的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解析和查找元素类型的建议。

2 个答案:

答案 0 :(得分:2)

[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.
    }

答案 1 :(得分:1)

message string媒体资源中读取Text

    [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; }
    }