【问题标题】:How to get attribute in the XDocument object如何在 XDocument 对象中获取属性
【发布时间】:2010-12-13 14:39:38
【问题描述】:

我有这个 xml

<config>
    <audio first="true" second="false" third="true" />
</config>

我希望我的代码能够做这样的事情

if (xdoc.getAttr("first")=="true")
    Console.Write("first is true");

如何使用 LINQ XDocument 执行此操作? 到目前为止,我所拥有的是加载了该 xml 字符串的 XDocument 对象。

【问题讨论】:

  • 我会回答,但这将是多余的。您要执行的操作在 XElement 中。
  • @Slaks,我做了,但我找不到我要找的东西,@legatou 我会看看 XElement。

标签: c# xml linq linq-to-xml


【解决方案1】:

需要获取&lt;audio&gt;元素的属性:

string value = xdoc.Root.Element("audio").Attribute("first").Value;

【讨论】:

  • 这个不错。我添加了 Convert.toBoolean(value);将其转换为布尔值
【解决方案2】:

你应该看看 XElement

article at c-sharpcorner.com

【讨论】:

    猜你喜欢
    • 2012-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-19
    • 2020-10-06
    相关资源
    最近更新 更多