本文介绍了XPath问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好的,这是交易。我有一个小的XML文件,代表一个小的数据库表。我将它加载到System.XML.XMLDocument中。到目前为止一直很好。 我对它运行XPath查询以检索所有字段名称。一切都好 工作正常。 这是我的XML文档: <?xml版本= QUOT; 1.0"独立= QUOT;是" ?> < DataSet1 xmlns =" http://www.tempuri.org/DataSet1.xsd"> < tblItem> < ID> 1< / ID> <名称>垃圾邮件< /名称> <类别>食物< /类别> < Description>美味!没有天然成分< /描述> <价格> 4< /价格> < ImageURL> images / 1.png< / ImageURL> < LargeImageURL> images / L1.png< / LargeImageURL> < / tblItem> < tblItem> < ; ID> 2< / ID> <名称>远程控制< /名称> <类别>其他< / Category> <描述>通用遥控器< /描述> <价格> 12< / Price> < ImageURL> images / 2.png< / ImageURL> < LargeImageURL> images / L2.png< / LargeImageURL> < / tblItem> < / DataSet> 现在是棘手的部分。我正在尝试提出三个XPath查询 将返回以下内容: 1)所有具有子ID节点的tblItem节点值为1(即,所有 tblItem,其中ID = 1), 2)所有具有杂项类别的tblItem节点(即,所有 tblItem其中Category =''Miscellaneous'') 3)所有在描述中都有单词Remote的tblItem ndoes(即所有 tblItem类别在任何位置都包含Remote这个词。 来自SQL背景,我很难实现XPath 表达式。我希望有人能指出我正确的方向。方向。我尝试了几种组合,例如tblItem / ID [。=''1'']", " tblItem [ID =''1'']"和// tblItem [ID =''Miscellaneous'']"。他们中的任何一个似乎都不会工作...... TIA OK, here''s the deal. I have a small XML file that represents a smalldatabase table. I load it into a System.XML.XMLDocument. So far so good.I run an XPath query against it to retrieve all the field names. Everythingthere works fine. Here''s my XML Document: <?xml version="1.0" standalone="yes" ?><DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd"><tblItem><ID>1</ID><Name>Spam</Name><Category>Food</Category><Description>Yummy! No natural ingredients</Description><Price>4</Price><ImageURL>images/1.png</ImageURL><LargeImageURL>images/L1.png</LargeImageURL></tblItem><tblItem><ID>2</ID><Name>Remote Control</Name><Category>Miscellaneous</Category><Description>Universal Remote</Description><Price>12</Price><ImageURL>images/2.png</ImageURL><LargeImageURL>images/L2.png</LargeImageURL></tblItem></DataSet> Now for the tricky part. I''m trying to come up with three XPath queriesthat will return the following: 1) All tblItem nodes that have a child ID node with a value of 1 (i.e., alltblItem where ID = 1),2) All tblItem nodes that have a Category of Miscellaneous (i.e., alltblItem where Category = ''Miscellaneous'')3) All tblItem ndoes that have the word Remote in the Description (i.e., alltblItem where Category contains the word ''Remote'' in any position) Coming from a SQL background, I''m having a hard time implementing XPathexpressions. I was hoping someone here could point me in the rightdirection. I''ve tried several combinations, like "tblItem/ID[.=''1'']","tblItem[ID=''1'']" and "//tblItem[ID=''Miscellaneous'']". None of them seem tobe working though... TIA 推荐答案 // tblItem [ID = 1] //tblItem[ID=1] // tblItem [Category =''Miscellaneous''] //tblItem[Category=''Miscellaneous''] 所有 我不知道这个。 你要记住一件事,如果你想在代码中这样做, I don''t know this one. One thing that you have to keep in mind, if you want to do this in code, 就是那个您必须为xpath查询设置名称空间管理器以包含已定义的名称空间。在您的示例中,您有一个默认命名空间。 如果你不提供命名空间管理器,那么你的查询总是返回零结果。 - --- Nick Malik [微软] MCSD,CFPS,认证Scrummaster http://blogs.msdn。 com / nickmalik 免责声明:本论坛中发表的意见是我自己的,而不是我雇主的代表。我代表我不回答问题雇主。我只是一名帮助程序员的程序员。 - Michael C# < xy*@abcdef.com>在消息中写道新闻:65 ***************** @ fe08.lga ... is that you have to set the namespace manager for the xpath query to include the defined namespaces. In your example, you have a default namespace.If you don''t provide the namespace manager, then your query will ALWAYSreturn zero results. -- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I''m just a programmer helping programmers. -- "Michael C#" <xy*@abcdef.com> wrote in message news:65*****************@fe08.lga... 所有 所有 似乎seem to 这篇关于XPath问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 14:56