我有XML,看起來像選擇從具有匹配某些情況c#LINQ
<?xml version="1.0"?>
<configuration>
<TemplateMapper>
<Template XML="Product.xml" XSLT="sheet.xslt" Keyword="Product" />
<Template XML="Cart.xml" XSLT="Cartsheet.xslt" Keyword="Cart" />
</TemplateMapper>
</configuration>
當我通過在屬性關鍵字的值爲「產品」我想LINQ到一個屬性的XML元素的屬性的多個值將XML和XSLT屬性的值返回爲字符串和字符串的字典。
到目前爲止我試過:
var Template="Product"
var dictionary = (from el in xmlElement.Descendants("TemplateMapper")
let xElement = el.Element("Template")
where xElement != null && xElement.Attribute("Keyword").Value == Template
select new
{
XML = el.Attribute("XML").Value,
XSLT= el.Attribute("XSLT").Value
}).ToDictionary(pair => pair.XML, pair => pair.XSLT);
KeyValuePair<string, string> templateValues = dictionary.FirstOrDefault();
它給是一個錯誤「不設置到對象的實例對象引用」。任何人都可以發現我在做什麼錯誤?非常感謝。
真棒非常感謝主席先生! – Bravo11