C#XPath的帮助 - 表达不工作 [英] C# XPath help - Expression not working

查看:176
本文介绍了C#XPath的帮助 - 表达不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是相匹配的,我正从信息的示例XML文档:

 < XML版本= ?1.0独立=YES> 
<产品的xmlns =http://tempuri.org/Products.xsd>
<影GT;
<标题>标题1< /标题>
<语言和GT;英语< /语言与GT;
< /影GT;
<影GT;
<标题>标题2< /标题>
<语言和GT;英语< /语言与GT;
< /影GT;
<影GT;
<标题>&TITLE3 LT; /标题>
<语言和GT;法语< /语言与GT;
< /影GT;
<图书>
<标题> BTitle1< /标题>
<&体裁GT;&悬念LT; /类型>
< /图书>
<图书>
<标题> BTitle2< /标题>
<&体裁GT;&悬念LT; /类型>
< /图书>
<图书>
<标题> BTitle3< /标题>
<&体裁GT;科幻< /类型>
< /图书>
<图书>
<标题> BTitle4< /标题>
<&体裁GT;科幻< /类型>
< /图书>
< /产品>

下面是我的代码来获得悬念的流派所有的书:

  //使用XPath 
XDOC的XPathDocument =新的XPathDocument(xmlpath中)获得国家清单; //路径我的文件
的XPathNavigator XNAV = xDoc.CreateNavigator();
串booksQuery =书籍[风格= \Suspense\];
声明XPathNodeIterator xIter = xNav.Select(booksQuery);

,而(xIter.MoveNext())
{
//做xIter.Current
的东西}

我尝试了好几种查询包括产品/书籍[风格= \Suspense\] 产品/图书 ./图书图书 。我xIter始终为零的项目。



我是新来的XPath,所以我敢肯定,这是一个非常简单的错误,但也许不是。我知道我可以得到表[电影]和一个DataSet [书籍]出使用 myDataSet.ReadXml(myXmlPathString)这个XML文件的; 所以XML文件未损坏。如果任何帮助。



所以,我的问题......我究竟做错了什么?


解决方案

  XDOC的XPathDocument =新的XPathDocument(xmlpath中); //路径我的文件
的XPathNavigator XNAV = xDoc.CreateNavigator();
的XmlNamespaceManager mngr =新的XmlNamespaceManager(xNav.NameTable);
mngr.AddNamespace(P,http://tempuri.org/Products.xsd);
串booksQuery =//号码:书籍[P:类型= \Suspense\];
声明XPathNodeIterator xIter = xNav.Select(booksQuery,mngr);


Here is an example XML doc that matches the one I am getting info from:

<?xml version="1.0" standalone="yes"?>
<Products xmlns="http://tempuri.org/Products.xsd">
  <Movies>
    <Title>Title1</Title>
    <Language>English</Language>
  </Movies>
  <Movies>
    <Title>Title2</Title>
    <Language>English</Language>
  </Movies>
  <Movies>
    <Title>Title3</Title>
    <Language>French</Language>
  </Movies>
  <Books>
    <Title>BTitle1</Title>
    <Genre>Suspense</Genre>
  </Books>
  <Books>
    <Title>BTitle2</Title>
    <Genre>Suspense</Genre>
  </Books>
  <Books>
    <Title>BTitle3</Title>
    <Genre>SciFi</Genre>
  </Books>
  <Books>
    <Title>BTitle4</Title>
    <Genre>SciFi</Genre>
  </Books>
</Products>

Here is my code to get all books with the genre of Suspense:

//Get state list using XPath
XPathDocument xDoc = new XPathDocument(xmlPath); //Path to my file
XPathNavigator xNav = xDoc.CreateNavigator();
string booksQuery = "Books[Genre = \"Suspense\"]";
XPathNodeIterator xIter = xNav.Select(booksQuery);

while (xIter.MoveNext())
{
    //do stuff with xIter.Current
}

I have tried several queries including Products/Books[Genre = \"Suspense\"], Products/Books, ./Books, and Books. My xIter always has zero items.

I'm new to XPath, so I'm sure it's a really simple error, but maybe not. I know I can get a DataSet with the tables [Movies] and [Books] out of this XML file using myDataSet.ReadXml(myXmlPathString); so the XML file is not corrupt. If that helps any.

So, my question ... what am I doing wrong?

解决方案

XPathDocument xDoc = new XPathDocument(xmlPath); //Path to my file
XPathNavigator xNav = xDoc.CreateNavigator();
XmlNamespaceManager mngr = new XmlNamespaceManager(xNav.NameTable);
mngr.AddNamespace("p", "http://tempuri.org/Products.xsd");
string booksQuery = "//p:Books[p:Genre = \"Suspense\"]";
XPathNodeIterator xIter = xNav.Select(booksQuery,mngr);

这篇关于C#XPath的帮助 - 表达不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆