在我的控制器中,我正在像这样将XML数据转换为JSON
var doc = new XmlDocument();
doc.Load(xmlStream);
doc.DocumentElement.RemoveAttribute("xmlns"); // don't want xmlns node in json
return Json(doc.DocumentElement.ChildNodes, new JsonSerializerSettings { StringEscapeHandling = StringEscapeHandling.Default }, Encoding.UTF8);
XML 如下所示
<Employees xmlns="http://company.com/schemas">
<Employee>
<FirstName>name1</FirstName>
<LastName>surname1</LastName>
<Employee>
<Employee>
<FirstName>name2</FirstName>
<LastName>surname2</LastName>
<Employee>
<Employee>
<FirstName>name3</FirstName>
<LastName>surname3</LastName>
<Employee>
</Employees>
并输出 JSON
[
{
"Employee": {
"FirstName": "name1",
"LastName": "surname1"
}
},
{
"Employee": {
"FirstName": "name2",
"LastName": "surname2"
}
},{
"Employee": {
"FirstName": "name3",
"LastName": "surname3"
}
}
]
我想要实现的目标
[
{
"FirstName": "name1",
"LastName": "surname1"
},
{
"FirstName": "name2",
"LastName": "surname2"
},{
"FirstName": "name3",
"LastName": "surname3"
}
]
那么有没有办法在将XML元素序列化为JSON的同时将其转换为JSON数组呢?我想使它动态化,所以我不想在这样做时编写实体类。
我离你的预期输出应该是多少。我认为我们可以做得更好,但这是我的两条线解决方案:
在线试用!
var doc = XDocument.Parse(xmlData);
var json = JsonConvert.SerializeXNode(doc);
输出:
{
"Employees": {
"@xmlns": "http://company.com/schemas",
"Employee": [{
"FirstName": "name1",
"LastName": "surname1"
}, {
"FirstName": "name2",
"LastName": "surname2"
}, {
"FirstName": "name3",
"LastName": "surname3"
}]
}
}
从@Peter编辑:
由于模式是常量,因此可以导航对象以访问对象:
var employees = JObject.Parse(json).Children().Children().Children().ElementAt(1).Children().First();
在线试用!
如果您之前在对象数组中反序列化它是可能的。像这样:
class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
static void DoXmlToJson()
{
string xmlData = @"<?xml version='1.0' encoding='UTF-8'?>
<Employees>
<Employee>
<FirstName>name1</FirstName>
<LastName>surname1</LastName>
</Employee>
<Employee>
<FirstName>name2</FirstName>
<LastName>surname2</LastName>
</Employee>
<Employee>
<FirstName>name3</FirstName>
<LastName>surname3</LastName>
</Employee>
</Employees>";
XDocument doc = XDocument.Parse(xmlData);
var array = doc.Root.Elements()
.Select(row => new Employee() {
FirstName = row.Element("FirstName").Value,
LastName = row.Element("LastName").Value
});
string json = JsonConvert.SerializeObject(array);
Console.WriteLine(json);
}
使用 XmlSerializer.Deserialze 的另一种方法:
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Employees
{
[XmlElement("Employee")]
public Employee[] Items { get; set; }
}
static void DoXmlToJson()
{
string xmlData = @"<Employees>
<Employee>
<FirstName>name1</FirstName>
<LastName>surname1</LastName>
</Employee>
<Employee>
<FirstName>name2</FirstName>
//<LastName>surname2</LastName>
</Employee>
<Employee>
<FirstName>name3</FirstName>
<LastName>surname3</LastName>
</Employee>
</Employees>";
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Employees));
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlData));
Employees wholeObject = (Employees)xmlSerializer.Deserialize(memoryStream);
string json = JsonConvert.SerializeObject(wholeObject.Items);
Console.WriteLine(json);
}
输出在这两种情况下看起来都是这样的:
[
{
"FirstName": "name1",
"LastName": "surname1"
},
{
"FirstName": "name2",
"LastName": "surname2"
},
{
"FirstName": "name3",
"LastName": "surname3"
}
]
使用 Cinchoo ETL - 一个开源库,您只需几行代码即可轻松地完成 Xml 到 Json
string xml = @"<Employees xmlns=""http://company.com/schemas"">
<Employee>
<FirstName>name1</FirstName>
<LastName>surname1</LastName>
</Employee>
<Employee>
<FirstName>name2</FirstName>
<LastName>surname2</LastName>
</Employee>
<Employee>
<FirstName>name3</FirstName>
<LastName>surname3</LastName>
</Employee>
</Employees>
";
StringBuilder sb = new StringBuilder();
using (var p = ChoXmlReader.LoadText(xml)
)
{
using (var w = new ChoJSONWriter(sb))
w.Write(p);
}
Console.WriteLine(sb.ToString());
输出:
[
{
"FirstName": "name1",
"LastName": "surname1"
},
{
"FirstName": "name2",
"LastName": "surname2"
},
{
"FirstName": "name3",
"LastName": "surname3"
}
]
查看代码项目文章以获取更多帮助。
免责声明:我是这个库的作者。