【问题标题】:Convert XML DATA TO JSON [duplicate]将 XML 数据转换为 JSON [重复]
【发布时间】:2016-04-05 12:22:21
【问题描述】:

我需要在后面的 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);

对此的任何帮助将不胜感激..

谢谢。

【问题讨论】:

  • 我不知道这个 Json 序列化器,但我认为你不能直接在里面放一个 XmlDocument 实例!?看起来更像是期待一个具有属性和值作为输入的具体类实例。
  • 感谢金的回复
  • 尝试使用 JsonConvert.SerializeXmlNode(xml)

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


【解决方案1】:

您需要按照 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# 实例。

这是使用 newtonsoft 将 xml 序列化为 json 的代码

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

将 xml 保存在文件中并从文件中加载而不是硬编码为字符串是一种很好的做法,这会使代码变得笨拙。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-10
        • 1970-01-01
        • 2017-10-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-13
        • 1970-01-01
        • 2013-01-28
        相关资源
        最近更新 更多