【问题标题】:How to get the attribute value of xlink:title from xml如何从xml中获取xlink:title的属性值
【发布时间】:2012-08-28 18:00:11
【问题描述】:

这是我为检索组成员而生成的 xml。我需要从此 xml 中获取值 tech\abc1234。

<tcm:Trustee xlink:href="tcm:0-61-65552" xlink:type="simple" xlink:title="tech\abc1234" Type="65552" Icon="T65552L0P0" xmlns:xlink="http://www.w3.org/1999/xlink"  xmlns:tcm="http://www.tridion.com/ContentManager/5.0"></tcm:Trustee>

但是当我尝试获取属性值时:

XElement userList = csClient.GetListXml(grpId, members);

foreach (var eachuser in userList.Elements())
       {
            logdetails(eachuser.Attribute("xlink:title").Value.ToString());
       }

我收到以下错误: error The ':' character, hexadecimal value 0x3A, cannot be included in a name.

【问题讨论】:

    标签: c# xml tridion tridion-2011


    【解决方案1】:

    目前,您正在使用stringXName 的转换,它将字符串作为只是元素的本地ID,并且不能包含冒号。

    您需要使用完整的命名空间 + 本地 ID 创建一个 XName。幸运的是,LINQ 让这一切变得非常简单:

    XNamespace xlink = "http://www.w3.org/1999/xlink";
    XElement userList = csClient.GetListXml(grpId, members);
    
    foreach (var user in userList.Elements())
    {
        logdetails(user.Attribute(xlink + "title").Value);
    }
    

    请注意,在Value 之后无需调用ToString() - XAttribute.Value 已经返回一个字符串。

    【讨论】:

      猜你喜欢
      • 2016-08-04
      • 2016-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      相关资源
      最近更新 更多