您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在XDocument中使用XPath?

如何在XDocument中使用XPath?

如果您拥有XDocument,则使用LINQ-to-XML会更容易:

var document = XDocument.Load(fileName);
var name = document.Descendants(XName.Get("Name", @"http://demo.com/2011/demo-schema")).First().Value;

如果确定XPath是唯一需要的解决方案,则:

using System.Xml.XPath;

var document = XDocument.Load(fileName);
var namespaceManager = new XmlNamespaceManager(new NaMetable());
namespaceManager.AddNamespace("empty", "http://demo.com/2011/demo-schema");
var name = document.XPathSelectElement("/empty:Report/empty:ReportInfo/empty:Name", namespaceManager).Value;
其他 2022/1/1 18:17:21 有449人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶