【问题标题】:XDocument getting Root Attributes valuesXDocument 获取根属性值
【发布时间】:2012-04-23 12:45:38
【问题描述】:

如何使用 xDocument 从数据表中的该字符串中获取 title1 的值

<Person ActionType = "Update"  Title1="Miss" />

我尝试了后代、XAttributes 和各种...也许输入是错误的,但是

XDocument xml = XDocument.Parse(row["XMLTransaction"].ToString());

IEnumerable<XAttribute> query =
from transaction in xml.Root.Elements()
select transaction.Attribute(attribute);

【问题讨论】:

    标签: c# .net xml linq-to-xml xattribute


    【解决方案1】:

    如果该字符串是您的文字 XML,那么您应该省略 .Elements() 部分。

    使用 XElement 而不是 XDocument 更短:

     var xml = XElement.Parse(row["XMLTransaction"].ToString());
    
     IEnumerable<XAttribute> query = xml.Attributes();
    

    【讨论】:

    • 好吧,谢谢。只是希望有一些网站教程-也许我需要改进我的谷歌搜索
    • 顺便问一下,我如何获得价值?我整个上午都在努力
    • query2 = xml.Attributes().Select(a =&gt; a.Value); 给出IEnum&lt;string&gt;
    • xattributes 不包含“选择”的定义。我可以使用 foreach 遍历它们,但想知道访问属性值的最佳方法
    • 是扩展方法,需要using System.Linq;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多