C# 获取 xelement 属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17789271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:29:03  来源:igfitidea点击:

get xelement attribute value

c#xmllinq-to-xmlxelement

提问by LFurness

I have an XElement that looks like this:

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

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

How can I use XML to extract the value of the LoginName attribute? I tried the following, but the q2 "Enumeration yielded no results".

如何使用 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);
}

采纳答案by Sergey Berezovskiy

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"

回答by LFurness

I ended up using string manipulation to get the value, so I'll post that code, but I would still like to see an XML approach if there is one.

我最终使用字符串操作来获取值,因此我将发布该代码,但我仍然希望看到一种 XML 方法(如果有的话)。

string strEl = el.ToString();
string[] words = strEl.Split(' ');
foreach (string word in words)
{
    if (word.StartsWith("LoginName"))
    {
        strEl = word;
        int first = strEl.IndexOf("\"");
        int last = strEl.LastIndexOf("\"");
        string str2 = strEl.Substring(first + 1, last - first - 1); 
        //str2 = "dn1\jdiaz"
    }
}

回答by sora0419

XmlDocument doc = new XmlDocument();
doc.Load("myFile.xml"); //load your xml file
XmlNode user = doc.getElementByTagName("User"); //find node by tag name  
string login = user.Attributes["LoginName"] != null ? user.Attributes["LoginName"].Value : "unknown login";

The last line of code, where it's setting the string login, the format looks like this...

最后一行代码,在那里设置string login,格式看起来像这样......

var variable = condition ? A : B;

It's basically saying that if condition is true, variable equals A, otherwise variable equals B.

基本上是说如果条件是true,变量等于 A,否则变量等于 B。

回答by adgteq

from the docs for XAttribute.Value:

来自 XAttribute.Value 的文档:

If you are getting the value and the attribute might not exist, it is more convenient to use the explicit conversion operators, and assign the attribute to a nullable type such as stringor Nullable<T>of Int32. If the attribute does not exist, then the nullable type is set to null.

如果您正在获取值并且该属性可能不存在,则使用显式转换运算符并将该属性分配给可空类型(例如stringor Nullable<T>of )会更方便Int32。如果该属性不存在,则可空类型设置为空。