c# - 使用 Linq to XML (C#) 如何查找属性值?

标签 c# xml linq

我有一个简单的 XML 文件,看起来像这样:

<Institutions>
    <FI name = "NameOne">
        <longname>some text</longname>
        <APIKey>some text</APIKey>
        <connectstring>some text</connectstring>
    </FI>
    <FI name = "NameTwo">
        <longname>some text</longname>
        <APIKey>some text</APIKey>
        <connectstring>some text </connectstring>
    </FI>
</Institutions>

使用 LINQ to XML 我可以抓取整个文件,找到“longname”、“APIKey”和“connectstring”的所有值,但我不知道如何找到所有“name”值或如何只抓取每个 FI 名称值下的三个信息。需要说明的是,我事先不知道 name= values 是什么。

我正在使用:

XElement root = XElement.Load("c:\\directory\\Data_Config.xml");

IEnumerable<XElement> Fis =
from el in root.Elements("Institutions")
select el;

根据 MSDN 文档加载文件。它的所有引用似乎都暗示我知道我要查询的名称值是什么。

我用谷歌搜索,尝试了不同的属性/元素查询,但都没有成功。我很确定这很简单,但它在逃避我。

如何获取这些数据?

谢谢,

杰森

最佳答案

var xml = XElement.Load (@"c:\directory\Data_Config.xml");
var query = 
    from e in xml.Descendants("FI")
    select e.Attribute("name").Value; 

关于c# - 使用 Linq to XML (C#) 如何查找属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7435652/

相关文章:

c# - 每个页面优化中的购物车

c# - 使用 MVVM 将 WPF 绑定(bind)到 Popup 控件中的 ComboBox

c# - 如何创建和使用 .NET 元数据 'Reference Assembly' ?

xml - XSL-FO 页码 2a、2b

linq - 编译的 LINQ 查询 - NHibernate

c# - 偶尔错误: The type 'HttpRequestMessage' is defined in an assembly that is not referenced

c# - XDocument.Element 在解析 xml 字符串时返回 null

c# - 使用 .NET 读取 XML 文件

c# - 如何检查两个对象的属性是否相等

c# - 在 LINQ 查询中调用 .AsParallel() 的位置