如何使用C#/ LINQ将XML转换为JSON?

我有以下XML文件,我需要在服务器中转换为JSON。 最初我以为我会将它转换为Dictionary然后使用JavaScriptSerializer将其转换为JSON,但由于每列可能有不同的值类型,我认为它不会起作用。 有没有人在C#/ LINQ之前做过类似的事情?

我需要保留每列的值类型(布尔值,字符串,整数)。

我很感激任何建议,因为我刚刚开始使用XML。 谢谢。

 True Hello World 999  

 using System; using System.Linq; using System.Web.Script.Serialization; using System.Xml.Linq; class Program { static void Main() { var xml = @" True Hello World 999 "; var dic = XDocument .Parse(xml) .Descendants("Column") .ToDictionary( c => c.Attribute("Name").Value, c => c.Value ); var json = new JavaScriptSerializer().Serialize(dic); Console.WriteLine(json); } } 

生产:

 {"key1":"True","key2":"Hello World","key3":"999"} 

显然,这会将所有值视为字符串。 如果要保留基础类型语义,可以执行以下操作:

 using System; using System.Linq; using System.Web.Script.Serialization; using System.Xml.Linq; class Program { static void Main() { var xml = @" True Hello World 999 "; var dic = XDocument .Parse(xml) .Descendants("Column") .ToDictionary( c => c.Attribute("Name").Value, c => Convert.ChangeType( c.Value, typeof(string).Assembly.GetType(c.Attribute("DataType").Value, true) ) ); var json = new JavaScriptSerializer().Serialize(dic); Console.WriteLine(json); } } 

生产:

 {"key1":true,"key2":"Hello World","key3":999} 

如果您无法修改基础XML结构,则需要一个自定义函数,该函数将在您的自定义类型和基础.NET类型之间进行转换:

 using System; using System.Linq; using System.Web.Script.Serialization; using System.Xml.Linq; class Program { static void Main() { var xml = @" True Hello World 999 "; var dic = XDocument .Parse(xml) .Descendants("Column") .ToDictionary( c => c.Attribute("Name").Value, c => Convert.ChangeType( c.Value, GetType(c.Attribute("DataType").Value) ) ); var json = new JavaScriptSerializer().Serialize(dic); Console.WriteLine(json); } private static Type GetType(string type) { switch (type) { case "Integer": return typeof(int); case "String": return typeof(string); case "Boolean": return typeof(bool); // TODO: add any other types that you want to support default: throw new NotSupportedException( string.Format("The type {0} is not supported", type) ); } } } 

是否有必要使用LINQ? 否则你可以尝试这个:

 XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc); 

摘自这篇文章 。

对于具有更多和未知属性的XML元素的深度嵌套,您可以使用此递归:

 private static string XmlToJson(string xmlString) { return new JavaScriptSerializer().Serialize(GetXmlValues(XElement.Parse(xmlString))); } private static Dictionary GetXmlValues(XElement xml) { var attr = xml.Attributes().ToDictionary(d => d.Name.LocalName, d => (object)d.Value); if (xml.HasElements) attr.Add("_value", xml.Elements().Select(e => GetXmlValues(e))); else if (!xml.IsEmpty) attr.Add("_value", xml.Value); return new Dictionary { { xml.Name.LocalName, attr } }; } 

对于您的示例,结果将是:

 { "Columns":{ "_value":[ { "Column":{ "Name":"key1", "DataType":"Boolean", "_value":"True" } }, { "Column":{ "Name":"key2", "DataType":"String", "_value":"Hello World" } }, { "Column":{ "Name":"key3", "DataType":"Integer", "_value":"999" } } ] } } 

对于更复杂的XML情况,您可以在此处检查JSON模拟。