0

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>

1 Answer 1

3
[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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.