Мне нужно разобрать ниже XML для json в коде C#.Преобразование XML-данных в JSON
<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>
Ниже приведен мой код на C#, который я использовал для преобразования XML в JSON, но он терпит неудачу. Я не могу сериализовать использование SerializeObject.
var xml = new XmlDocument();
xml.LoadXml(ABOVE XMLSTRING);
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.None);
Любая помощь по этому вопросу будет очень высоко ценится ..
Спасибо.
Я не знаю этот сериализатор Json, но я думаю, что вы не можете прямо поставить экземпляр 'XmlDocument' там !? Похоже, он ожидает, что конкретный экземпляр класса со свойствами и значениями будет использоваться как вход. –
Спасибо Ким за ваш ответ – Anbarasi
Попробуйте использовать JsonConvert.SerializeXmlNode (xml) – Kevin