C# 如何在c中使用LINQ比较xml中的口述键和xml属性值?

C# 如何在c中使用LINQ比较xml中的口述键和xml属性值?,c#,xml,linq,C#,Xml,Linq,我有一个口述样本,其中包含 1 data1 2 data2 3 data3 4 data4 以及xml filesample.xml,格式如下: <node> <element id="1" value="val1"/> <element id="2" value="val2"/> <element id="3" value="val3"/> <element id="4" value="val4"/> <elemen

我有一个口述样本,其中包含

1 data1
2 data2
3 data3
4 data4
以及xml filesample.xml,格式如下:

<node>
 <element id="1" value="val1"/>
 <element id="2" value="val2"/>
 <element id="3" value="val3"/>
 <element id="4" value="val4"/>
 <element id="5" value="val5"/>
 <element id="6" value="val6"/>
 <element id="7" value="val7"/>
</node>
我需要将口述键与xml属性id匹配,并插入匹配的id和 将attributevalue的值转换为另一个听写式

现在我用的是:

XmlDocument XDOC = new XmlDocument();
XDOC.Load("Sample.xml");
XmlNodeList NodeList = XDOC.SelectNodes("//element");
Dictionary<string, string> dictTwo = new Dictionary<string, string>();
foreach (string l_strIndex in dictSample .Keys)
        {
            foreach (XmlNode XNode in NodeList)
            {
                XmlElement XEle = (XmlElement)XNode;
                if (dictSample[l_strIndex] == XEle.GetAttribute("id"))
                    dictTwo.Add(dictSample[l_strIndex], XEle.GetAttribute("value").ToString());
            }
        }

请帮助我使用LINQ以一种简单的方式执行此操作。您可能需要:

var q = from x in NodeList.Cast<XmlElement>()
    join k in dictSample on x.GetAttribute("id") equals k.Value
    select new { Key = k.Value, Value = x.GetAttribute("value").ToString() };

dictTwo = q.ToDictionary(x => x.Key);

你可能想要这个:

var q = from x in NodeList.Cast<XmlElement>()
    join k in dictSample on x.GetAttribute("id") equals k.Value
    select new { Key = k.Value, Value = x.GetAttribute("value").ToString() };

dictTwo = q.ToDictionary(x => x.Key);