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") ?? "");