2015-10-30 101 views
0

我有2種不同類型的需要使用單個類序列化的xml文件。 我無法在序列化之前使用xml解析器。這是MSMQ的典型反序列化方案。XML反序列化:相同的元素名稱,但類型不同

問題在於提供的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解析和查找的建議元件類型。

+0

只需添加以下到複雜類型:XMLTEXT] 公共字符串文本{獲得;組; } 您仍然可以在不使用字符串的情況下反序列化。 – jdweng

回答

1

message stringText屬性:

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

我不認爲這將有可能符合我的要求。我們不能使所有對象都成爲CompleType – Binod

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. 
    } 
+0

我不認爲這將有可能符合我的要求。我們不能將所有對象設置爲CompleType – Binod

相關問題