【问题标题】:XML Deserialization : Same element name but different typeXML反序列化:相同的元素名称但不同的类型
【发布时间】:2015-10-30 02:40:57
【问题描述】:

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

【问题讨论】:

  • 只需在 Complextype 中添加以下内容:[XmlText] public String Text { get;放; } 你仍然可以在没有字符串的情况下反序列化。
  • 你有解决办法吗?

标签: c# xml serialization deserialization xml-deserialization


【解决方案1】:
[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.
    }

【讨论】:

  • 我认为这不可能满足我的要求。我们不能将所有对象都设为 CompleType
【解决方案2】:

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

【讨论】:

  • 我认为这不可能满足我的要求。我们不能将所有对象都设为 CompleType
猜你喜欢
  • 2017-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2020-01-14
  • 2015-10-08
相关资源
最近更新 更多