如何在不指定属性名称的情况下动态检索属性名称?

发布于 2024-09-14 02:25:37 字数 854 浏览 3 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌倦 2024-09-21 02:25:37

看来您正在寻找类似的东西:

// ...
select new
{
    SlotName = SLT.Attributes().First().Name,
    SlotValue = SLT.Attributes().First().Value
};

It seems you are looking for something like:

// ...
select new
{
    SlotName = SLT.Attributes().First().Name,
    SlotValue = SLT.Attributes().First().Value
};
空‖城人不在 2024-09-21 02:25:37

如果我理解正确的话,您始终可以将变量传递给 LINQ 查询:

var string attrName = "NAME";  // specify whatever value you need ...

// wrap the query below in a function, if it will be reused...
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(attrName).Value,  
                     SlotValue = (string)SLT.Attribute(attrName).Value  
                 }; 

If I understand you correctly, you could always pass a variable in to the LINQ query:

var string attrName = "NAME";  // specify whatever value you need ...

// wrap the query below in a function, if it will be reused...
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(attrName).Value,  
                     SlotValue = (string)SLT.Attribute(attrName).Value  
                 }; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文