使用C#将XML转化为JSON
C#中有个可以把XML转化为JSON的包:Newtonsoft.Json。
官方文档地址:https://www.newtonsoft.com/json/help/html/Introduction.htm
先写一个类来专门做XML解析:(ps.别忘了using Newtonsoft.Json,如果报错去NuGet Package Manager搜一下然后安装一下就ok了,篇幅考虑,直接从类定义开始贴代码)
public class XmlParser
{
public XmlParser() { }
public string XmlStringToJson(string xml)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
string json = JsonConvert.SerializeXmlNode(doc);
return json;
}
public string XmlWebToJson(string url)
{
string resultset = new HttpHepler().HttpClientRequest(url);
return this.XmlStringToJson(resultset);
}
}
发http的类:
public class HttpHepler
{
public HttpHepler()
{
}
//通过httpClient发送请求的函数
public string HttpClientRequest(st