How to convert XML to JSON using C#/LINQ?(如何使用 C#/LINQ 将 XML 转换为 JSON?)
问题描述
我有以下需要在服务器中转换为 JSON 的 XML 文件.最初我以为我会将它转换为字典,然后使用 JavaScriptSerializer 将其转换为 JSON,但由于每列可能有不同的值类型,我认为它不会起作用.以前有人在 C#/LINQ 中做过类似的事情吗?
I have the following XML file that I need to convert to JSON in the server. Initially I thought I would convert it to a Dictionary and then use the JavaScriptSerializer to turn it into JSON but since each column could have a different value type, I don't think it would work. Has anyone done something similar before in C#/LINQ?
我需要保留每列的值类型(布尔、字符串、整数).
I need to preserve the Value Types(Boolean, String, Integer) of each column.
如果我刚刚开始使用 XML,我将不胜感激.谢谢.
I would appreciate any advice on this as Im just starting to work with XML. Thanks.
<Columns>
<Column Name="key1" DataType="Boolean">True</Column>
<Column Name="key2" DataType="String">Hello World</Column>
<Column Name="key3" DataType="Integer">999</Column>
</Columns>
推荐答案
using System;
using System.Linq;
using System.Web.Script.Serialization;
using System.Xml.Linq;
class Program
{
static void Main()
{
var xml =
@"<Columns>
<Column Name=""key1"" DataType=""Boolean"">True</Column>
<Column Name=""key2"" DataType=""String"">Hello World</Column>
<Column Name=""key3"" DataType=""Integer"">999</Column>
</Columns>";
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"}
显然,这会将所有值都视为字符串.如果您想保留底层类型语义,您可以执行以下操作:
Obviously this treats all the values as strings. If you want to keep the underlying type semantics you could do the following:
using System;
using System.Linq;
using System.Web.Script.Serialization;
using System.Xml.Linq;
class Program
{
static void Main()
{
var xml =
@"<Columns>
<Column Name=""key1"" DataType=""System.Boolean"">True</Column>
<Column Name=""key2"" DataType=""System.String"">Hello World</Column>
<Column Name=""key3"" DataType=""System.Int32"">999</Column>
</Columns>";
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 类型之间进行转换:
And if you cannot modify the underlying XML structure you will need a custom function that will convert between your custom types and the underlying .NET type:
using System;
using System.Linq;
using System.Web.Script.Serialization;
using System.Xml.Linq;
class Program
{
static void Main()
{
var xml =
@"<Columns>
<Column Name=""key1"" DataType=""Boolean"">True</Column>
<Column Name=""key2"" DataType=""String"">Hello World</Column>
<Column Name=""key3"" DataType=""Integer"">999</Column>
</Columns>";
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)
);
}
}
}
这篇关于如何使用 C#/LINQ 将 XML 转换为 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 C#/LINQ 将 XML 转换为 JSON?


- 输入按键事件处理程序 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01