1

[Update: Added some more details as I spotted more complexity to it!]

I need help modeling an API response that has CDATA everywhere. It looks like this:

<MYAPI>
    <SomeItem>
        <Description>
            <![CDATA[
              //Something here
            ]]>
            <InnerDescription>some string here</InnerDescription>
            <InnerDescription somethingType = "some type">
                <![CDATA[
                    //Something here
                ]]>
            </InnerDescription>
        </Description>
    </SomeItem>
</MYAPI>

I want to create c# classes for this response but I'm clueless as to how to model the sections that have CDATA. And also the <InnerDescription> </InnerDescription> have cdata content and string content. How would I model the following classes?

public class Description {
    ??
}

public class InnerDescription {
    //Has both cdata and string
    ?? 
}

Thank You and I appreciate your help!

Ash K
  • 1,802
  • 17
  • 44
  • Copy the XML, Open Visual Studio, In a new file, Click Edit > Paste Special > Paste XML as classes – Daniel Mar 09 '19 at 20:21
  • Hi Daniel, What kind of file should I create new? A .cs file? And how to paste XML as classes? Thank You – Ash K Mar 09 '19 at 20:27
  • And I can only see "Paste JSON as classes" when I try to paste something into a .cs file – Ash K Mar 09 '19 at 20:53
  • 1
    `CDATA` is just a way to encode textual data that uses an alternate form of escaping for markup characters. You can model a `CDATA` section as a string. Now if for whatever reason you need to serialize back to `CDATA` rather than a simple string with escaped markup characters then you need to do a little work, see [How do you serialize a string as CDATA using XmlSerializer?](https://stackoverflow.com/q/1379888). – dbc Mar 09 '19 at 21:00
  • Hi dbc, I can't really make out how to create model for this, from that link you provided. Can you please show me one, like for my problem? Thanks! – Ash K Mar 11 '19 at 02:43

0 Answers0