作者:dv18183995666 | 来源:互联网 | 2023-05-21 18:20
1> Giorgos Bets..:
用这个:
using System.Xml.Linq; // required namespace for linq-to-xml
/* ... get xml into 'body' string */
XDocument doc = XDocument.Parse(body);
将XML文件加载到XDocument
对象中.
然后,您可以使用Linq-to-XML来解析XML和ToDictionary
扩展方法,以便为XML的每个属性创建一个键/值对:
var output = doc.Element("ncresponse")
.Attributes()
.Select(c => new {
Key = c.Name,
Value = c.Value
})
.ToDictionary(k => k.Key, v => v.Value);
看来我的事情过于复杂(归功于@KyleW).这个:
var output = doc.Element("ncresponse")
.Attributes()
.ToDictionary(k => k.Name, v => v.Value);
相当于初始linq查询.Select
只有在需要对字典中的值进行一些预处理时才需要.
输出继电器:
[0] = {[NCERRORPLUS, !]}
[1] = {[BRAND, ABC]}
[2] = {[PM, CC]}
... etc