Here is an example XML document that matches the one with which I am getting the information:
<?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 the Suspense books:
//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 tried several queries, including Products/Books[Genre = \"Suspense\"] , Products/Books , ./Books and Books . My xIter always has null elements.
I am new to XPath, so I'm sure this is a really simple mistake, but maybe not. I know that I can get a DataSet with tables [Films] and [Books] from this XML file using myDataSet.ReadXml(myXmlPathString); so that the XML file is not corrupted. If it helps anyone.
So my question is ... what am I doing wrong?
c # xml xpath
Mike webb
source share