Patrick

Patrick

  • NA
  • 3
  • 0

Deserialize XML - Ignore Element - HowTo?

Dec 8 2019 12:56 PM
Hello
 
I have a XML, like this(there could be many more Message elements):
  1. <xml version="1.0" encoding="utf-8"?>    
  2. <response>    
  3.     <Count>2Count>    
  4.     <Messages>    
  5.         <Message>    
  6.             <Smstat>0Smstat>    
  7.             <Index>40005Index>    
  8.             <Phone>+4xxxxxxPhone>    
  9.             <Content>Test 2Content>    
  10.             <Date>2019-12-08 18:13:35Date>    
  11.             <Sca>Sca>    
  12.             <SaveType>4SaveType>    
  13.             <Priority>0Priority>    
  14.             <SmsType>1SmsType>    
  15.         Message>    
  16.         <Message>    
  17.             <Smstat>1Smstat>    
  18.             <Index>40004Index>    
  19.             <Phone>+4xxxxxx>    
  20.             <Content>Test 1Content>    
  21.             <Date>2019-12-08 18:13:30Date>    
  22.             <Sca>Sca>    
  23.             <SaveType>4SaveType>    
  24.             <Priority>0Priority>    
  25.             <SmsType>1SmsType>    
  26.        </Message>    
  27.     </Messages>    
  28. </response>
I'd like to deserialize it, I came up with this code:
  1. [Serializable()]  
  2.     [System.Xml.Serialization.XmlRoot("Messsages")]  
  3.     public class Messsages  
  4.     {  
  5.         [XmlArray("Messsages")]  
  6.         [XmlArrayItem("Message"typeof(Message))]  
  7.         public Message[] Car { getset; }  
  8.     }  
  1. [Serializable()]  
  2.     public class Message  
  3.     {  
  4.         [System.Xml.Serialization.XmlElement("Smstat")]  
  5.         public int Smstat { getset; }  
  6.   
  7.         [System.Xml.Serialization.XmlElement("Index")]  
  8.         public int Index { getset; }  
  9.   
  10.         [System.Xml.Serialization.XmlElement("Phone")]  
  11.         public string Phone { getset; }  
  12.   
  13.         [System.Xml.Serialization.XmlElement("Content")]  
  14.         public string Content { getset; }  
  15.   
  16.         [System.Xml.Serialization.XmlElement("Date")]  
  17.         public string Date { getset; }  
  18.   
  19.         [System.Xml.Serialization.XmlElement("sca")]  
  20.         public string sca { getset; }  
  21.   
  22.         [System.Xml.Serialization.XmlElement("SaveType")]  
  23.         public string SaveType { getset; }  
  24.   
  25.         [System.Xml.Serialization.XmlElement("Priority")]  
  26.         public string Priority { getset; }  
  27.   
  28.         [System.Xml.Serialization.XmlElement("SmsType")]  
  29.         public string SmsType { getset; }  
  30.     }  
  1. Messsages[] messages = null;  
  2.             XmlSerializer serializer = new XmlSerializer(typeof(Message[]));  
  3.   
  4.             using (var response = (HttpWebResponse)request.GetResponse())  
  5.             {  
  6.                 messages = (Messsages[])serializer.Deserialize(response.GetResponseStream());  
  7.             }  
If I do it like this I get an InvalidOperationException telling me that the xml doesn't match due to the "response"
 
If I have a look on my XML the Messages are inside "" - so far it makes sense.
 
My question now is, how can I "ignore" that "response"? It is unneeded information for me and I just like to finally have only the data that I defined in the two data classes above.
 
Is there some ignore flag or how could I achieve that?
 
Thank you for your help!

Answers (1)