简体   繁体   中英

XML CDATA reading in C#

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>
[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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM