-1

I need to parse the below XML to json in C# code behind.

<rss version="2.0">
<channel>
<title>TITLE </title>
<link>http://sample.com/</link>
<language>en-Us</language>
<pubDate>System.String[] GMT</pubDate>
<item>
<ParentSection>News</ParentSection>
<ParentSectionTamilname>செய்திகள்</ParentSectionTamilname>
<ParentSectionID>1</ParentSectionID>
<Count>6</Count>
<items>
<Section>National</Section>
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
<Sectionid>3</Sectionid>
<SectionURL>
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1
</SectionURL>
<SectionJsonURL>
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1
</SectionJsonURL>
<subitem>
<Sub_Count>0</Sub_Count>
</subitem>
</items>
<items>
<Section>National</Section>
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
<Sectionid>3</Sectionid>
<SectionURL>
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1
</SectionURL>
<SectionJsonURL>
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1
</SectionJsonURL>
<subitem>
<Sub_Count>0</Sub_Count>
</subitem>
</items>
</item>

<item>
<ParentSection>News</ParentSection>
<ParentSectionTamilname>செய்திகள்</ParentSectionTamilname>
<ParentSectionID>1</ParentSectionID>
<Count>6</Count>
<items>
<Section>National</Section>
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
<Sectionid>3</Sectionid>
<SectionURL>
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1
</SectionURL>
<SectionJsonURL>
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1
</SectionJsonURL>
<subitem>
<Sub_Count>0</Sub_Count>
</subitem>
</items>
<items>
<Section>National</Section>
<Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
<Sectionid>3</Sectionid>
<SectionURL>
http://sample.com/rss/RssfeedXML.aspx?Id=3&Main=1
</SectionURL>
<SectionJsonURL>
http://sample.com/json/JsonfeedXML.aspx?Id=3&Main=1
</SectionJsonURL>
<subitem>
<Sub_Count>0</Sub_Count>
</subitem>
</items>
</item>
</channel>
</rss>

Below is my C# code which i used to convert the XML to JSON, but it fails. I cannot serialize using SerializeObject.

var xml = new XmlDocument();
                xml.LoadXml(ABOVE XMLSTRING);
                string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.None);

Any help on this would be very much appreciated..

Thank you.

Anbarasi
  • 421
  • 2
  • 5
  • 23

3 Answers3

5

You should have used JsonConvert.SerializeXmlNode() instead to serialize XML to JSON :

var xml = new XmlDocument();
xml.LoadXml("your XML here");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml);
har07
  • 88,338
  • 12
  • 84
  • 137
5

You need to format your xml string as per xml standards.

&amp; // Use instead of &

The formatted xml string is below

<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
  <channel>
    <title>TITLE </title>
    <link>http://sample.com/</link>
    <language>en-Us</language>
    <pubDate>System.String[] GMT</pubDate>
    <item>
        <ParentSection>News</ParentSection>
        <ParentSectionTamilname>செய்திகள்</ParentSectionTamilname>
        <ParentSectionID>1</ParentSectionID>
        <Count>6</Count>
        <items>
            <Section>National</Section>
            <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
            <Sectionid>3</Sectionid>
            <SectionURL>
                http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1
            </SectionURL>
            <SectionJsonURL>
                http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1
            </SectionJsonURL>
            <subitem>
                <Sub_Count>0</Sub_Count>
            </subitem>
        </items>
        <items>
            <Section>National</Section>
            <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
            <Sectionid>3</Sectionid>
            <SectionURL>
                http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1
            </SectionURL>
            <SectionJsonURL>
                http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1
            </SectionJsonURL>
            <subitem>
                <Sub_Count>0</Sub_Count>
            </subitem>
        </items>
    </item>

    <item>
        <ParentSection>News</ParentSection>
        <ParentSectionTamilname>செய்திகள்</ParentSectionTamilname>
        <ParentSectionID>1</ParentSectionID>
        <Count>6</Count>
        <items>
            <Section>National</Section>
            <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
            <Sectionid>3</Sectionid>
            <SectionURL>
                http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1
            </SectionURL>
            <SectionJsonURL>
                http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1
            </SectionJsonURL>
            <subitem>
                <Sub_Count>0</Sub_Count>
            </subitem>
        </items>
        <items>
            <Section>National</Section>
            <Sectiontamil>தேசியச்செய்திகள்</Sectiontamil>
            <Sectionid>3</Sectionid>
            <SectionURL>
                http://sample.com/rss/RssfeedXML.aspx?Id=3&amp;Main=1
            </SectionURL>
            <SectionJsonURL>
                http://sample.com/json/JsonfeedXML.aspx?Id=3&amp;Main=1
            </SectionJsonURL>
            <subitem>
                <Sub_Count>0</Sub_Count>
            </subitem>
        </items>
    </item>
</channel>

You need to call the SerializeXmlNode method not serialize object. SerializeObject is used to convert xml to c# instance.

Here's the code to serialize xml to json using newtonsoft

  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load("InputXml.xml"); // Can use xmlDoc.LoadXml(YourString);
  string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);

It is a good practice to save your xml in a file and load from a file rather than hard code as string which makes code clumsy.

Dilip Nandakumar
  • 198
  • 2
  • 14
1

You have to fix your xml first. It is not possible to parse it. Use VS to spot the errors. Also, you have special characters of url addresses you have to fix.

Secret20
  • 11
  • 3