2015-10-30 2 views
0

У меня есть 2 разных типа xml-файлов, которые нужно сериализовать с помощью одного класса. Я не могу использовать синтаксический анализатор xml до сериализации. Это типичный сценарий десериализации MSMQ.XML Deserialization: имя одного и того же типа, но различное.

Проблема связана с узлом message_string в xml.

<?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

Просто добавьте следующее ComplexType: [XmlText] общественного String Текст {получить; задавать; } Вы все еще можете десериализовать без строки. – jdweng

ответ

1

Read 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; } 
    } 
+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

Смежные вопросы