将 XML 数据转换为 JSON
Convert XML DATA TO 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>
下面是我用来将 XML 转换为 JSON 的 C# 代码,但它失败了。我无法使用 SerializeObject 进行序列化。
var xml = new XmlDocument();
xml.LoadXml(ABOVE XMLSTRING);
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(xml, Newtonsoft.Json.Formatting.None);
如有任何帮助,我们将不胜感激..
谢谢。
您必须先修复您的 xml。无法解析它。使用 VS 发现错误。此外,您必须修复 url 个地址的特殊字符。
您应该使用 JsonConvert.SerializeXmlNode()
而不是将 XML 序列化为 JSON :
var xml = new XmlDocument();
xml.LoadXml("your XML here");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml);
您需要按照 xml 标准格式化 xml 字符串。
& // 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&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>
您需要调用 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 保存在文件中并从文件中加载,而不是将硬代码作为字符串加载,这会使代码变得笨拙。
我需要在后面的 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);
如有任何帮助,我们将不胜感激..
谢谢。
您必须先修复您的 xml。无法解析它。使用 VS 发现错误。此外,您必须修复 url 个地址的特殊字符。
您应该使用 JsonConvert.SerializeXmlNode()
而不是将 XML 序列化为 JSON :
var xml = new XmlDocument();
xml.LoadXml("your XML here");
string jsonString = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml);
您需要按照 xml 标准格式化 xml 字符串。
& // 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&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>
您需要调用 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 保存在文件中并从文件中加载,而不是将硬代码作为字符串加载,这会使代码变得笨拙。