[英]c# xml -> dictionary <string, Tuple<string,string,string>> in Linq
I have this XML Document 我有这个XML文件
<?xml version="1.0" encoding="utf-8"?>
<Tag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<data ID="1" Tag1="A" Tag2="123" Tag3="C" />
<data ID="2" Tag1="AB" Tag2="12C3" Tag3="D" />
</Tag>
I want the convert the document into Dictionary Type <string, Tuple<string,string,string>>
我想将文档转换为字典类型<string, Tuple<string,string,string>>
So basically ID -> Tag1, Tag2, Tag3 所以基本上是ID-> Tag1,Tag2,Tag3
I know there are a lot that do Key -> Value like this: 我知道有很多事情要做Key-> Value这样的:
var configDictionary = xdoc.Descendants("data").ToDictionary(
datum => datum.Attribute("ID").Value,
datum => datum.Attribute("value").Value);
But i need have the value take in 3 strings. 但是我需要让值接受3个字符串。
This is rather simple. 这很简单。 All you're missing is a declaration of the tuple using Tuple.Create
: 您所缺少的只是使用Tuple.Create
声明元组:
var configDictionary = xdoc.Descendants("data")
.ToDictionary(
datum => datum.Attribute("ID").Value,
datum => Tuple.Create(datum.Attribute("Tag1").Value,
datum.Attribute("Tag2").Value,
datum.Attribute("Tag3").Value));
Note this has no validation that the attributes actually exists, which will need to be added. 请注意,这没有验证属性是否实际存在,需要添加该属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.