如何在不指定属性名称的情况下动态检索属性名称?
我正在开发 asp.net 移动应用程序。我正在使用 LINQ to XML 来查询 XML 文件。我正在使用以下查询来检索名称和名称;动态查询的值如下
var TotalManifolds = from MF in FieldRoot.Element("FIELD-DEFINITION").Element("MANIFOLDS").Elements("MANIFOLD")
join SLT in FieldRoot.Element("FIELD-DEFINITION").Element("SLOTS").Elements("SLOT")
on (string)MF.Attribute("MID") equals (string)SLT.Attribute("PARENT")
select new
{
SlotName = (string)SLT.Attribute("NAME").Value,
SlotValue = (string)SLT.Attribute("NAME").Value
};
在上述查询的以下语句中,我想动态检索属性的名称,而不显式指定属性的名称
SlotName = (string)SLT.Attribute("NAME").Value
这里我显式指定名称。我想要编写可以动态检索属性名称的代码。我是 Linq to xml 新手。您能告诉我如何以编程方式完成此操作吗?或者您能给我提供解决上述问题的链接吗?
I am developing asp.net mobile application. I am using LINQ to XML to query XML file. I am using the following query to retrieve the name & value of the query dynamically as follows
var TotalManifolds = from MF in FieldRoot.Element("FIELD-DEFINITION").Element("MANIFOLDS").Elements("MANIFOLD")
join SLT in FieldRoot.Element("FIELD-DEFINITION").Element("SLOTS").Elements("SLOT")
on (string)MF.Attribute("MID") equals (string)SLT.Attribute("PARENT")
select new
{
SlotName = (string)SLT.Attribute("NAME").Value,
SlotValue = (string)SLT.Attribute("NAME").Value
};
In the following statement of above query I want to retrieve the name of the attribute dynamically without explicitly specifying the name of the attribute
SlotName = (string)SLT.Attribute("NAME").Value
Here I am explicitly specifying the name. I want to code which can dynamically retrieve the name of the attribute. I am new to Linq to xml. Can you please tell how this can be done programatically ? or can you provide me the link through which I can resolve the above issue ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您正在寻找类似的东西:
It seems you are looking for something like:
如果我理解正确的话,您始终可以将变量传递给 LINQ 查询:
If I understand you correctly, you could always pass a variable in to the LINQ query: