Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the attribute value of xlink:title from xml

This is my xml generated for retrieving members of a group. I need to get the value tech\abc1234 from this xml.

<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>

But when i try to get the attribute value like:

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

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

I am getting the following error : error The ':' character, hexadecimal value 0x3A, cannot be included in a name.

like image 514
pavan Avatar asked Dec 18 '25 10:12

pavan


1 Answers

Currently, you're using the string to XName conversion, which takes that string as just the local ID of an element, and that can't contain a colon.

You need to create an XName with the full namespace + local ID. Fortunately, LINQ makes that really easy:

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);
}

Note that there's no need to call ToString() after Value - XAttribute.Value already returns a string.

like image 59
Jon Skeet Avatar answered Dec 21 '25 01:12

Jon Skeet