我有一个看起来像这样的XElement:

<User ID="11" Name="Juan Diaz" LoginName="DN1\jdiaz" xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/" />

如何使用XML提取LoginName属性的值?我尝试了以下操作,但是q2“枚举未产生结果”。
var q2 = from node in el.Descendants("User")
    let loginName = node.Attribute(ns + "LoginName")
    select new { LoginName = (loginName != null) };
foreach (var node in q2)
{
    Console.WriteLine("LoginName={0}", node.LoginName);
}

最佳答案

var xml = @"<User ID=""11""
                  Name=""Juan Diaz""
                  LoginName=""DN1\jdiaz""
                  xmlns=""http://schemas.microsoft.com/sharepoint/soap/directory/"" />";

var user = XElement.Parse(xml);
var login = user.Attribute("LoginName").Value; // "DN1\jdiaz"

10-07 14:19