【问题标题】:How to get the xml attribute value of root?如何获取root的xml属性值?
【发布时间】:2012-01-13 03:54:33
【问题描述】:

问:

如何通过LINQ获取根元素(我的xml文件中的第一个元素)的属性值。

.cs :

 XDocument xmlDoc = XDocument.Load(targetFileName);

.xml:

<timetable ascttversion="2010"  options="idprefix:realID">

我想读取options 的值。

【问题讨论】:

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


    【解决方案1】:

    以下应该做的

    xmlDoc.Root.Attribute("option").Value
    

    【讨论】:

      【解决方案2】:

      类似这样的:

      XDocument xdoc = XDocument.Load(targetFileName);
      var attrib = xdoc.Root.Attribute("options").Value;
      
      // attrib = "idprefix:realID"
      

      【讨论】:

      • hmmmm,我想从文件中读取options 值,我的意思是使用xmlDoc
      • 因为根据您的代码,options 将始终是相同的值
      • 对,您应该只执行 XDocument.Load() 而不是 XDocument.Parse() ,因为您已经拥有 - 我只是以 XDocument.Parse() 为例。我可以更新示例代码。
      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 2011-07-26
      • 2012-02-28
      • 2016-08-01
      • 1970-01-01
      • 2016-08-04
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多