将XML数据转换为JSON

时间:2016-04-05 12:22:22

标签: c# json xml windows-phone-8.1

我需要在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>

下面是我用来将XML转换为JSON的C#代码,但它失败了。我无法使用SerializeObject进行序列化。

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

对此有任何帮助将非常感谢..

谢谢。

3 个答案:

答案 0 :(得分:4)

您应该使用JsonConvert.SerializeXmlNode()来将XML序列化为JSON:

var xml = new XmlDocument();
xml.LoadXml("your XML here");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml);

答案 1 :(得分:4)

您需要按照xml标准格式化xml字符串。

&amp; // Use instead of &

格式化的xml字符串位于

之下
<?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>

您需要调用SerializeXmlNode方法而不是序列化对象。 SerializeObject用于将xml转换为c#instance。

这是使用newtonsoft

将xml序列化为json的代码
  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load("InputXml.xml"); // Can use xmlDoc.LoadXml(YourString);
  string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);

将xml保存在文件中并从文件而不是硬编码加载为字符串会使代码变得笨拙是一个好习惯。

答案 2 :(得分:1)

您必须先修复xml。无法解析它。使用VS发现错误。此外,您还需要修复网址的特殊字符。