C# 如何获取XElement的值
我正在通过xml文档进行解析,需要获取C# 如何获取XElement的值,c#,C#,我正在通过xml文档进行解析,需要获取属性元素的值。现在,我有一行代码返回: <Property name="ID" value="thevalueineed"/> 我缺少的下一个步骤是什么,我将从我在var ID中的元素中获得所需的值?只需将您的选择为 select (string)el.Attribute("value") 工作代码 var xDoc = XDocument.Parse(@"<root><Property name=""ID"" value=
属性
元素的值。现在,我有一行代码返回:
<Property name="ID" value="thevalueineed"/>
我缺少的下一个步骤是什么,我将从我在
var ID
中的元素中获得所需的值?只需将您的选择为
select (string)el.Attribute("value")
工作代码
var xDoc = XDocument.Parse(@"<root><Property name=""ID"" value=""thevalueineed""/></root>");
var ID = from el in xDoc.Root.DescendantsAndSelf("Property")
where (string)el.Attribute("name") == "ID"
select (string)el.Attribute("value");
var val = ID.First();
var xDoc=XDocument.Parse(@“”);
var ID=从xDoc.Root.DegenantsAndSelf(“属性”)中的el开始
其中(字符串)el.Attribute(“name”)=“ID”
选择(字符串)el.属性(“值”);
var val=ID.First();
只需将选择更改为
select (string)el.Attribute("value")
工作代码
var xDoc = XDocument.Parse(@"<root><Property name=""ID"" value=""thevalueineed""/></root>");
var ID = from el in xDoc.Root.DescendantsAndSelf("Property")
where (string)el.Attribute("name") == "ID"
select (string)el.Attribute("value");
var val = ID.First();
var xDoc=XDocument.Parse(@“”);
var ID=从xDoc.Root.DegenantsAndSelf(“属性”)中的el开始
其中(字符串)el.Attribute(“name”)=“ID”
选择(字符串)el.属性(“值”);
var val=ID.First();
这应该行得通
var ID = from el in linkedinfo.Descendants("Property")
where el.Attribute("name").Value == "ID"
select el.Attribute("value").Value;
这应该行得通
var ID = from el in linkedinfo.Descendants("Property")
where el.Attribute("name").Value == "ID"
select el.Attribute("value").Value;
我个人会使用.Value
而不是转换为字符串:
XElement linkedTeethInfo = XElement.Parse(@"<Property name=""ID"" value=""thevalueineed""/>");
var ID = from el in linkedTeethInfo.DescendantsAndSelf("Property")
where el.Attribute("name").Value == "ID"
select el.Attribute("value").Value;
Console.WriteLine("ID: {0}", ID.First());
XElement-linkedTeethInfo=XElement.Parse(@“”);
变量ID=来自linkedTeethInfo.DegenantsAndSelf(“属性”)中的el
其中el.Attribute(“name”).Value==“ID”
选择el.属性(“值”).值;
WriteLine(“ID:{0}”,ID.First());
在这里创建了一个小提琴:我个人会使用.Value
而不是使用字符串:
XElement linkedTeethInfo = XElement.Parse(@"<Property name=""ID"" value=""thevalueineed""/>");
var ID = from el in linkedTeethInfo.DescendantsAndSelf("Property")
where el.Attribute("name").Value == "ID"
select el.Attribute("value").Value;
Console.WriteLine("ID: {0}", ID.First());
XElement-linkedTeethInfo=XElement.Parse(@“”);
变量ID=来自linkedTeethInfo.DegenantsAndSelf(“属性”)中的el
其中el.Attribute(“name”).Value==“ID”
选择el.属性(“值”).值;
WriteLine(“ID:{0}”,ID.First());
在这里创建了一个小提琴:我个人喜欢这种读取XML文件的方法,但我不知道它是否适用于您的XML文件
// Create a DataSet
DataSet ds = new DataSet();
// Get the data from the XML file
ds.ReadXml("C:\\myXMLFile.xml");
// Get values
foreach (DataRow dr in ds.Tables[0].Rows)
{
string value = dr["ID"].ToString();
}
我个人在阅读XML文件时喜欢这种方法,但我不知道它是否适用于您的XML文件
// Create a DataSet
DataSet ds = new DataSet();
// Get the data from the XML file
ds.ReadXml("C:\\myXMLFile.xml");
// Get values
foreach (DataRow dr in ds.Tables[0].Rows)
{
string value = dr["ID"].ToString();
}
将返回一个类型
System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Xml.Linq.XElement,System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>>
将返回一个类型
System.Linq.Enumerable.WhereSelectEnumerableIterator<System.Xml.Linq.XElement,System.Collections.Generic.IEnumerable<System.Xml.Linq.XAttribute>>
我个人会使用.Value而不是转换为字符串
为什么?若value
属性不存在,它将抛出异常,并将错误隐藏在哪里。强制转换将返回null。有效点。强制转换可能会节省您的时间,但根据需要,您更希望出现异常,而不是我个人使用的“静默”故障切换。值而不是强制转换为字符串
为什么?若value
属性不存在,它将抛出异常,并将错误隐藏在哪里。强制转换将返回null。有效点。强制转换可能会节省您的时间,但根据需要,您更希望出现异常,而不是像您所说的那样出现“无声”故障切换,并且它不会返回正确的值。相反,它返回[0]=“System.Xml.Linq.XElement+d_u0”
您确定已将属性更改为属性吗?谢谢,这确实有效。我必须在.Attributes
中丢失is,并将其设置为属性
我按照您所说的做了,但它没有返回正确的值。相反,它返回[0]=“System.Xml.Linq.XElement+d_u0”
您确定已将属性更改为属性吗?谢谢,这确实有效。我不得不在.Attributes
中丢失is,并使其成为属性