問(wèn)題描述
我有以下需要在服務(wù)器中轉(zhuǎn)換為 JSON 的 XML 文件.最初我以為我會(huì)將它轉(zhuǎn)換為字典,然后使用 JavaScriptSerializer 將其轉(zhuǎn)換為 JSON,但由于每列可能有不同的值類(lèi)型,我認(rèn)為它不會(huì)起作用.以前有人在 C#/LINQ 中做過(guò)類(lèi)似的事情嗎?
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?
我需要保留每列的值類(lèi)型(布爾、字符串、整數(shù)).
I need to preserve the Value Types(Boolean, String, Integer) of each column.
如果我剛剛開(kāi)始使用 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);
}
}
產(chǎn)生:
{"key1":"True","key2":"Hello World","key3":"999"}
顯然,這會(huì)將所有值都視為字符串.如果您想保留底層類(lèi)型語(yǔ)義,您可以執(zhí)行以下操作:
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);
}
}
產(chǎn)生:
{"key1":true,"key2":"Hello World","key3":999}
如果您無(wú)法修改底層 XML 結(jié)構(gòu),您將需要一個(gè)自定義函數(shù),該函數(shù)將在您的自定義類(lèi)型和底層 .NET 類(lèi)型之間進(jìn)行轉(zhuǎn)換:
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)
);
}
}
}
這篇關(guān)于如何使用 C#/LINQ 將 XML 轉(zhuǎn)換為 JSON?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!