从c#/ xpath获取属性值
我有一个app.config文件,需要获取属性的值:
Liquid XML Studio提供以下xml:
/configuration/log4net/appender/param[1]
但是,C#代码可以使用xpath来获取值?
使用此XPath:
/configuration/log4net/appender/param[@name='File']/@value
根据您阅读XML的方式,使用XPath可能会略有不同。 如果您正在使用XDocument
,则可以使用XPathSelectElement
扩展方法。 如果您使用的是XmlDocument
,则会有一个SelectSingleNode
方法。 如果使用XPathDocument
,则需要编译XPathExpression
并将其用于导航器。
您可以使用XmlDocument
。 请参阅XmlNode.SelectSingleNode
和其他人。
例:
XmlDocument doc = new XmlDocument(); doc.LoadXml(@" "); var node = doc.DocumentElement.SelectSingleNode("//param[@name = 'File']/@value"); Console.WriteLine(node.Value);
就如 ….
var result = XDocument.Load("test.xml").Descendants("param"); foreach (var p in result) { Console.WriteLine(p.Attribute("name")); } Console.Read();
您可以使用XmlDocument和方法SelectSingleNode – http://msdn.microsoft.com/en-us/library/fb63z0tw.aspx
它会找到一个与您的XPath匹配的节点。
我会用LINQ to XML(不是XPath)来做这个
- Treeview上的VirtualizingStackPanel不是虚拟化
- 返回由Image.FromStream(Stream stream)方法创建的图像
- ASP.NET web api无法获得application / x-www-form-urlencoded HTTP POST
- 找出IEnumerable 是否具有唯一值的最佳方法
- 设置背景颜色或WPF(4.0)ListBox – Windows 8
- Oracle.DataAccess.Client.OracleException ORA-03135:连接丢失联系
- WPF中的INotifyPropertyChanged
- 如何用Reflection + C#获取所有引用
- 如何在c#中存储从C ++函数返回的uint8_t *变量?