简体   繁体   English

C#XML-&gt;字典 <string, Tuple<string,string,string> &gt;在临q

[英]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.

相关问题 元组vs字符串作为C#中的字典键 - Tuple vs string as a Dictionary key in C# C#转换列表 <string> 到字典 <string, string> LINQ - C# Convert List<string> to Dictionary<string, string> LINQ C#更新字典 <string,string> 使用LINQ查询 - C# Update a Dictionary<string,string> with LINQ Query 如何使用LINQ将结果作为Dictionary返回 <string,string> 在C#中 - How to use LINQ to return the result as Dictionary<string,string> in C# C#将JSON反序列化为字典 <int,Tuple<string,int> &gt; - C# deserializing JSON to Dictionary<int,Tuple<string,int>> 通过 C# 字典<string,string>进入 TypeScript Map<string,string></string,string></string,string> - Passing C# Dictionary<string,string> into TypeScript Map<string,string> XML转换为字符串(C#) - XML to string (C#) 使用EF,C#,Linq构建对象的集合(词典 <string,List<string,string> &gt;) - Build a collection of object using EF, C#, linq (Dictionary<string,List<string,string>>) 我的问题是关于比较列表 <string> 带字典 <string, List<string> &gt;使用C#Linq - My question is about comparing List<string> with Dictionary <string, List<string>> using C# linq C# - Lambda / LINQ - 从字典转换 <string, string> []到列表 <string[]> - C# - Lambda / LINQ - Conversion From Dictionary<string, string>[] to List<string[]>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM