c# - 使用 C# 读取 XML

标签 c# xml

<Tasks>
    <AuxFiles>
        <FileType AttachmentType='csv' FileFormat ='*.csv'>
    </AuxFiles>
</Tasks>

如果我知道 AttachmentType,在 C# 中获取 FileFormat 的语法是什么?

我们将不胜感激任何帮助。

最佳答案

我会使用 LINQ to XML:

var doc = XDocument.Load("file.xml");

var format = doc.Descendants("FileType")
                .Where(x => (string) x.Attribute("AttachmentType") == type)
                .Select(x => (string) x.Attribute("FileFormat"))
                .FirstOrDefault();

如果没有匹配的元素,或者第一个具有匹配的 AttachmentTypeFileType 没有 ,这将给出 null >FileFormat 属性。

关于c# - 使用 C# 读取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7001752/

相关文章:

c# - 如何使用 System.XML 在 C# 中注释一行 XML 文件

c# - Resharper 和 Productivity Power Tools 争夺我的使用权。我怎样才能停止流血?

c# - 将程序集加载到单独的 AppDomain 中,获取 InvalidCastException

c# - WPF DataGrid 行标题可见性错误

android - 如何使用 RippleView 创建涟漪?

xml - 从另一个包扩展结构时的非导出字段

java - TextView 阻止底部导航

c# - 如何在 VS2008(自动属性)中使用 "short style"属性设置默认值?

c# - 在 wcf 的客户端禁用证书验证

c# - XML 文件而不是 XML 字符串