I have an app.config file, and need to get value of an attribute:
我有一个app.config文件,需要获取属性值:
<param name="File" value="C:\"/>
Liquid XML Studio gives the following xml:
Liquid XML Studio提供了以下XML:
/configuration/log4net/appender/param[1]
However, what C# code can use xpath to get a value?
但是,什么c#代码可以使用xpath来获取值?
4 个解决方案
#1
16
Use this XPath:
使用这个XPath:
/configuration/log4net/appender/param[@name='File']/@value
Depending on how you read the XML, using the XPath may differ a bit. If you're using XDocument
, you can use the XPathSelectElement
extension method. If you're using XmlDocument
, there is a SelectSingleNode
method. And if you use an XPathDocument
, you need to compile a XPathExpression
and the use this against a navigator.
根据您如何读取XML,使用XPath可能略有不同。如果使用XDocument,可以使用XPathSelectElement扩展方法。如果使用XmlDocument,则有一个SelectSingleNode方法。如果使用XPathDocument,则需要编译XPathExpression并将其用于导航器。
#2
4
You can use XmlDocument
. See XmlNode.SelectSingleNode
and others.
您可以使用XmlDocument。看到XmlNode。SelectSingleNode等等。
Example:
例子:
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<configuration>
<log4net>
<appender>
<param name=""File"" value=""C:\""/>
</appender>
</log4net>
</configuration>");
var node = doc.DocumentElement.SelectSingleNode("//param[@name = 'File']/@value");
Console.WriteLine(node.Value);
#3
1
It like ....
就像....
var result = XDocument.Load("test.xml").Descendants("param");
foreach (var p in result)
{
Console.WriteLine(p.Attribute("name"));
}
Console.Read();
#4
0
You can use XmlDocument and a method SelectSingleNode - http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx
It will find a node matching your XPath.
您可以使用XmlDocument和一个方法SelectSingleNode—http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx,它将找到与XPath匹配的节点。
I would do this with LINQ to XML (not with XPath)
我将使用LINQ到XML(而不是XPath)
#1
16
Use this XPath:
使用这个XPath:
/configuration/log4net/appender/param[@name='File']/@value
Depending on how you read the XML, using the XPath may differ a bit. If you're using XDocument
, you can use the XPathSelectElement
extension method. If you're using XmlDocument
, there is a SelectSingleNode
method. And if you use an XPathDocument
, you need to compile a XPathExpression
and the use this against a navigator.
根据您如何读取XML,使用XPath可能略有不同。如果使用XDocument,可以使用XPathSelectElement扩展方法。如果使用XmlDocument,则有一个SelectSingleNode方法。如果使用XPathDocument,则需要编译XPathExpression并将其用于导航器。
#2
4
You can use XmlDocument
. See XmlNode.SelectSingleNode
and others.
您可以使用XmlDocument。看到XmlNode。SelectSingleNode等等。
Example:
例子:
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<configuration>
<log4net>
<appender>
<param name=""File"" value=""C:\""/>
</appender>
</log4net>
</configuration>");
var node = doc.DocumentElement.SelectSingleNode("//param[@name = 'File']/@value");
Console.WriteLine(node.Value);
#3
1
It like ....
就像....
var result = XDocument.Load("test.xml").Descendants("param");
foreach (var p in result)
{
Console.WriteLine(p.Attribute("name"));
}
Console.Read();
#4
0
You can use XmlDocument and a method SelectSingleNode - http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx
It will find a node matching your XPath.
您可以使用XmlDocument和一个方法SelectSingleNode—http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx,它将找到与XPath匹配的节点。
I would do this with LINQ to XML (not with XPath)
我将使用LINQ到XML(而不是XPath)