Reputation: 1320
I have below XML structure that is coming from the external service. That contains CDATA and it in turn have xml. I want to deserailize the CDATA content to C# object. Could anyone help me on this? I have gone through many article could't found the right one.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<Response xmlns=""><![CDATA[<result><Item1>Some data</Item1><Item2>Some data</Item2><Item3>Some data</Item3></result>]]></Response>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Below XML inside CDATA needs to be deserialized.
<result><Item1>Some data</Item1><Item2>Some data</Item2><Item3>Some data</Item3></result>
Upvotes: 0
Views: 1233
Reputation: 14251
[XmlRoot("result")]
public class Result
{
public string Item1 { get; set; }
public string Item2 { get; set; }
public string Item3 { get; set; }
}
Use:
Result result;
using (var xmlReader = XmlReader.Create(inputStream))
{
xmlReader.ReadToFollowing("Response");
xmlReader.Read(); // read CDATA tag
using (var stringReader = new StringReader(xmlReader.Value))
{
var xs = new XmlSerializer(typeof(Result));
result = (Result)xs.Deserialize(stringReader);
}
}
Upvotes: 3