I'm writing a de-serializer using XmlSerializer .Deserialize. The basic de-serializer works so I won't clutter the question with that code. I've written a class to store the data. I successfully de-serialize a node such as
<TransactionDate>2013-11-20T18:26:35.363</TransactionDate>
using
public string TransactionDate { get; set; }
but how would I deserailize these nodes to extract the data
<Product version="1.1.0" name="Product Name"/>
or
<Warning message="A Warning message here"/>
I'm consuming an existing service so I can't change how the input xml is formatted.
I've tried using the XmlElement attribute
[XmlElement("Product version")]
public string Productversion { get; set; }
or
[XmlElement("Warning message="A Warning message here"/>)]
public List<string> Message { get; set; } }
The second one has escape character problems, both return null.
How do I de-serialize nodes that contain data in the actual tag <.....>? What would the property on my class look like?
UPDATE: Following fcuesta's suggestion (no need for the XmlElement attribute and it didn't make any difference) I tried
public class Product { public string version { get; set; } public string name { get; set; } }
The members still do not get populated and are null.
UPDATE 2: Full Answer
Jason's answer worked. Both his code and when I applied the [XmlRoot()]
to my code. But the Xml is actually more nested and also has tags where data is in the tag and between the tags (the Answer node). Here's the Xml:
<PlatformResponse>
<Response>
<Questions>
<Question text="Which one of the following area codes is associated with you?" type="1">
<Answer correct="false">813</Answer><Answer correct="false">352</Answer>
<Answer correct="true">305/786</Answer><Answer correct="false">850</Answer>
<Answer correct="false">256</Answer><Answer correct="false">205</Answer>
<Answer correct="false">912</Answer><Answer correct="false">615</Answer><Answer correct="false">478</Answer>
<Answer correct="false">None of the above</Answer>
</Question>
<Question text="Which one of the following counties is associated with you?" type="2">
<Answer correct="false">Benton</Answer><Answer correct="true">Miami-Dade</Answer>
<Answer correct="false">Burke</Answer><Answer correct="false">Lafayette</Answer>
<Answer correct="false">Monroe</Answer><Answer correct="false">Dickson</Answer>
<Answer correct="false">Coosa</Answer><Answer correct="false">Smith</Answer>
<Answer correct="false">Muscogee</Answer><Answer correct="false">None of the above</Answer>
</Question>
<Question text="Which one of the following zip codes is associated with you?" type="3">
<Answer correct="false">33271</Answer>
<Answer correct="false">33929</Answer>
<Answer correct="false">33927</Answer>
<Answer correct="false">33007</Answer>
<Answer correct="true">33055</Answer>
<Answer correct="false">33061</Answer>
<Answer correct="false">33556</Answer>
<Answer correct="false">33263</Answer>
<Answer correct="false">33356</Answer>
<Answer correct="false">None of the above</Answer>
</Question>
</Response>
</PlatformResponse>
Here are my classes:
[XmlRoot("PlatformResponse")]
public class IDChckRspn
{
//public TransactionDetails TransactionDetails { get; set; }
public Response Response { get; set; }
}
public class Response
{
public Questions Questions { get; set; }
}
public class Questions
{
[XmlElement("Question")]
public List<Question> Question { get; set; }
}
public class Question
{
[XmlAttribute("text")]
public string Text { get; set; }
[XmlAttribute("type")]
public string Type { get; set; }
[XmlElement("Answer")]
public List<Answer> Answer { get; set; }
}
public class Answer
{
[XmlAttribute("correct")]
public string Correct { get; set; }
[XmlText()]
public string Text { get; set; }
}
Here's my de-serializer:
XmlSerializer s = new XmlSerializer(typeof(IDChckRspn));
StreamReader r = new StreamReader(@"c:\temp\response.xml");
object obj = s.Deserialize(r);
IDChckRspn _response = (IDChckRspn)obj;
This successfully de-serializes down the tree and the Answer tag that has data in the tag and between the tags. Note: This was accomplished through the use of four different attributes to decorate the various classes and members.
[XmlRoot("PlatformResponse")] [XmlElement("Question")] [XmlAttribute("text")] [XmlText()]