C# c Linq for XML:tag.substands不允许询问所有的后代

C# c Linq for XML:tag.substands不允许询问所有的后代,c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,我对这个网站相当陌生,这是我的第一个问题。我阅读了文档,但如果我违反了任何行为准则,我会事先表示歉意。 我的问题是: 我在流中有一个XML文件。我的目标是获取属性名、类型和键,它们是因为明显的原因而更改的 <YourKey> <Product_Key Name="eMbedded Visual C++ 4.0"> <Key ID="5" Type="Static Activation Key" ClaimedDate="">BBBBB-QW36D-DPT6T-

我对这个网站相当陌生,这是我的第一个问题。我阅读了文档,但如果我违反了任何行为准则,我会事先表示歉意。 我的问题是:

我在流中有一个XML文件。我的目标是获取属性名、类型和键,它们是因为明显的原因而更改的

<YourKey>
<Product_Key Name="eMbedded Visual C++ 4.0">
<Key ID="5" Type="Static Activation Key" ClaimedDate="">BBBBB-QW36D-DPT6T-BBBB-ZZZZZ</Key>
</Product_Key>
<Product_Key Name="Windows 10 Home">
<Key ID="1251" Type="Retail" ClaimedDate="1/25/2017">ZZZZZ-6GBG7-ZZZZZ-8JG23-FM47H</Key>
<Key ID="1251" Type="Retail" ClaimedDate="8/23/2016">FEFEF-FVD7K-EEEEF-BEBEB-VMHX7</Key>
<Key ID="1251" Type="Retail" ClaimedDate="4/28/2016">EEZZE-GYB6P-ZZZEE-R72PQ-EEEEZ</Key>
</Product_Key>
</YourKey>
有人知道如何查询我的文件来填充我的类吗


提前谢谢

您的键不是字符串,而是元素,您无法将其转换为字符串。根据您想要得到什么,您可以执行以下操作:

 Key = (List<string>)keys.Select(x=>x.Value).ToList()

如果你想得到类型,你应该考虑,你有不止一个类型的PRO产品。您可以将类型作为键查询到列表中,也可以确保它们都是相同的,因此可以只使用第一个:

var list = from product in productName.Descendants("Product_Key")
           let name = product.Attribute("Name")            
           let keys = product.Elements("Key")
           select new MsProduct
           {
               Name = (string)name,
               Type = keys.First().Attribute("Type").Value,
               Key = (List<string>)keys.Select(x=>x.Value).ToList()
           };
你不需要这些陈述。您确实需要从键中获取值

var list = from product in productName.Descendants("Product_Key")
               where product.Attribute("Name").Value == "YourKey"
             //  let name = product.Attribute("Name")
             //  let type = product.Attribute("Type")
             //  let keys = product.Elements("Key")
               select new MsProduct
               {
                   Name = (string)product.Attribute("Name"),
                   Type = product.Elements("Type").First().Attribute("Type").Value,
                   Key = product.Elements("Key").Select(e=>e.Value).ToList()
               };
但是ElementsType。首先当然是一个有问题的定义。
没有ProductKey的类型这样的东西。它可以有许多类型。

类型是键的属性,而不是产品的属性。注释该行并检查。还有一件事,您的XML无效。谢谢您的快速回答。XML直接来自Microsoft,它是我的MSDN子脚本的导出。我没有创建代码;。答案如下,我现在想知道如何完成打字。谢谢你的快速回答。这解决了键列表的问题,但我的类型仍然为空。我将其更改为:let type=productName.Degenantskey.AttributeType type=stringtype.Selectx=>x.Value.ToString,但现在,类型的值为type System.Linq.Enumerable+whereSelectEnumerableInterator`2[System.Xml.Linq.XAttribute,System.String]字符串您希望将什么视为类型?例如静态激活密钥?是的,我在文件中有静态激活密钥、零售或多重激活,我需要为我的软件提供这些信息。它确实解决了我的类型问题。非常感谢你!!!!你介意我问一下为什么这是一个有问题的定义吗?因为XML显然允许一个产品密钥使用不同的类型。所以说这里的类型是把基数弄错了。
 Key = (List<string>)keys.Select(x=>x.ToString()).ToList()
var list = from product in productName.Descendants("Product_Key")
var list = from product in productName.Descendants("Product_Key")
           let name = product.Attribute("Name")            
           let keys = product.Elements("Key")
           select new MsProduct
           {
               Name = (string)name,
               Type = keys.First().Attribute("Type").Value,
               Key = (List<string>)keys.Select(x=>x.Value).ToList()
           };
var list = from product in productName.Descendants("Product_Key")
               where product.Attribute("Name").Value == "YourKey"
             //  let name = product.Attribute("Name")
             //  let type = product.Attribute("Type")
             //  let keys = product.Elements("Key")
               select new MsProduct
               {
                   Name = (string)product.Attribute("Name"),
                   Type = product.Elements("Type").First().Attribute("Type").Value,
                   Key = product.Elements("Key").Select(e=>e.Value).ToList()
               };