【问题标题】:Reading XML nodes attribute as per user input根据用户输入读取 XML 节点属性
【发布时间】:2013-02-05 12:38:18
【问题描述】:
<Default>
<SharepointContentType id="SharePointContentType">

<name id="Test1"
         DisplayName="TestOne"
         SharepointGroup="Test1SPGp">
    </name>

<name id="Test2"
         DisplayName="TestTwo"
          SharepointGroup="Test2SPGp">
    </name>
.
.
.
.

</SharepointContentType>
.
.
.
.
<Default>

对于我想要下降的上述模式,找到一个节点,其中 id 是 SharePointContentType,而不是找到标签 &lt;name &gt;,其中 id 是 Test1(用户定义),然后获取元素 SharepointGroup 的值

例如,如果用户输入是 Test1 而输出应该是 Test1SPGp 请帮助我尝试使用 linq 并设法编写如下代码,但未能成功。

    XDocument xDoc = XDocument.Load(GetDefaultXMLFile());

    var feeds = from feed in xDoc.Descendants("name")
                where feed.Attribute("id").Equals("admin")
                select new
                {
                    fxfv = feed.Attribute("SharepointGroup").Value
                };

【问题讨论】:

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


    【解决方案1】:

    我认为你的 where 条件有误,见下文:

    feed.Attribute("id") 返回 XAttribute 类,因此您不能使用 equals 与字符串进行比较,但在 Value 属性之后可以。

            var feeds = from feed in xDoc.Descendants("name")
                        where feed.Attribute("id").Value.Equals("Test1")
                        select new
                        {
                            fxfv = feed.Attribute("SharepointGroup").Value
                        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      相关资源
      最近更新 更多