如何使用XPath用的XDocument?(how to use XPath with XDocument)

   IT问题网   2018-12-04 00:00:00

问 题

还有一个类似的问题,但似乎该解决方案并没有在我的情况制定出:<一href="http://stackoverflow.com/questions/3715936/weirdness-with-xdocument-xpath-and-namespaces">weirdness用的xdocument,xpath和命名空间

下面是我有工作的xml:

  xml版本="1.0"编码="utf-8"&gt?;
报告id为"id1"type ="试玩报告"创建="2011-01-01t01:01:01 + 11:00"文化="en"的xmlns ="http://demo.com/2011/demo -schema"
 reportinfo
名称试玩报告 /名称
 createdby单位测试 / createdby
 / reportinfo
 /报告
 

和下方则是code,我认为它应该工作,但事实并非如此......

xdoc的xdocument = xdocument.load(@"c:\ samplexml.xml");
xmlnamespacemanager的xnm =新的xmlnamespacemanager(新nametable());
xnm.addnamespace(的string.empty"http://demo.com/2011/demo-schema");
console.writeline(xdoc.xpathselectelement("/报告/ reportinfo /名称",xnm)== null);
 

有没有人有什么想法? 谢谢你。

解决方案

如果您的xdocument它更容易使用linq到xml:

var文件= xdocument.load(文件名);
var名称= document.descendants(xname.get("名称",@"http://demo.com/2011/demo-schema"))第一()值。;
 

如果您确信xpath是你需要的唯一的解决办法:

 使用system.xml.xpath;

var文件= xdocument.load(文件名);
var namespacemanager =新的xmlnamespacemanager(新nametable());
namespacemanager.addnamespace("空","http://demo.com/2011/demo-schema");
var名称= document.xpathselectelement("/空:报告/空:reportinfo /空:名称",namespacemanager).value的;
 

标签:如何使用



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

什么是WebConfigurationManager和ConfigurationManager中的区别?(What&#39;s the difference between the WebConfigurationManager and the ConfigurationManager)

problem what's the difference between the webconfiguratio ...

NUnit的测试运行秩序(NUnit Test Run Order)

problem by default nunit tests run alphabetic ...

如何恢复从C#数据库(How to restore a database from C#)

problem i have a sql 2008 db. i am running a form that ba ...

调用堆栈不说&QUOT;你来自哪里&QUOT;但是&QUOT;你要去哪里,下一个&QUOT;?(The call stack does not say &quot;where you came from&quot;, but &quot;where you are going next&quot;)

problem in a previous question (get object ca ...

如何读取和写入ID3标签在C#中的MP3?(How to read and write ID3 tags to an MP3 in C#)

problem is there a library for reading and writing id3 ta ...

如何设置读取权限的X.509证书从.NET私钥文件(How to set read permission on the private key file of X.509 certificate from .NET)

problem here is the code to add a pfx to the cert store. ...

获取列表视图的项目双击事件(Get the item doubleclick event of listview)

problem how to get the item double click event of listvie ...

MongoDB的交易?(MongoDB transactions)

problem playing around with mongodb and norm in .net. th ...

确定是否.NET程序集是从相同的源代码构建(Determine whether .NET assemblies were built from the same source)

problem does anyone know of a way to compare two .net ass ...

如何prevent更新一个ListViewItem的的文字时,在闪烁的ListView?(How to prevent flickering in ListView when updating a single ListViewItem&#39;s text)

problem all i want is to update an listviewit ...

谁应该调用Dispose IDisposable的对象时传递到另一个对象?(Who should call Dispose on IDisposable objects when passed into another object)

problem is there any guidance or best practic ...

获得一天的后缀使用则DateTime.ToString时,()(Getting day suffix when using DateTime.ToString())

problem is it possible to include the day suffix when for ...

改变目标框架我在Visual Studio解决方案的所有项目(Change the Target Framework for all my projects in a Visual Studio Solution)

problem i need to change the target framework ...

string.IsNullOrEmpty(串)与string.IsNullOrWhiteSpace(串)(string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string))

problem is use of string.isnullorempty(string ...

上传文件使用C#到ftp(Upload file to ftp using c#)

problem i try upload a file to an ftp-server with c#. the ...

什么是App.config中呢?(What is app.config for)

problem settings.settings generates settings.designer.cs ...

我可以使用ASP.NET MVC与常规ASP.NET Web表单(Can I use ASP.NET MVC together with regular ASP.NET Web forms)

problem i have on request from a client built a huge site ...

更优雅的异常处理不是多个catch块?(More Elegant Exception Handling Than Multiple Catch Blocks)

problem using c#, is there a better way to handle multipl ...

有什么问题确实IStructuralEquatable和IStructuralComparable解决?(What problem does IStructuralEquatable and IStructuralComparable solve)

problem i've noticed these two interfaces, an ...

正确的方法来延迟任务的开始(Correct way to delay the start of a Task)

problem i want to schedule a task to start in ...