Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有相应属性名称的情况下检索XML属性的值?_C#_Xml_Linq To Xml - Fatal编程技术网

C# 如何在没有相应属性名称的情况下检索XML属性的值?

C# 如何在没有相应属性名称的情况下检索XML属性的值?,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我将XDocument与C#一起使用 我有以下XML数据,我想从中提取ID(c5946、cdb9fb等): 但这也带来了: value="c5946", value="cdb9fb", etc. 而不是 c5946, cdb9fb, etc. 如何在没有相应属性名称的情况下获取属性值?使用属性的.Value属性 var allId = document.Descendants("ID").Select(id => id.Attribute("value").Value); 或者您可

我将
XDocument
与C#一起使用

我有以下XML数据,我想从中提取ID(c5946、cdb9fb等):

但这也带来了:

 value="c5946", value="cdb9fb", etc.
而不是

c5946, cdb9fb, etc.

如何在没有相应属性名称的情况下获取属性值?

使用属性的
.Value
属性

var allId = document.Descendants("ID").Select(id => id.Attribute("value").Value);
或者您可以将
XAttribute
转换为
字符串

var allId = document.Descendants("ID").Select(id => (string)id.Attribute("value"));
当元素中不存在属性时,转换将是一种更简单的方法

var allId = document.Descendants("ID").Select(id => (string)id.Attribute("value") ?? "");

使用属性的
.Value
属性

var allId = document.Descendants("ID").Select(id => id.Attribute("value").Value);
或者您可以将
XAttribute
转换为
字符串

var allId = document.Descendants("ID").Select(id => (string)id.Attribute("value"));
当元素中不存在属性时,转换将是一种更简单的方法

var allId = document.Descendants("ID").Select(id => (string)id.Attribute("value") ?? "");