Friday, 2 January 2015

Xpath using an attribute as a variable in an xml file where there is a namespace



If we have this xml file and I want to use the attribute Author as a variable to get the title Emma I know that I must write this string au = "Tommy"; string query = String.Format("//bookstore/book[@author={0}]/title", au); If we also have this example and I want to get the title Emma I know that I must write this XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("bk", "http://www.test.com/"); XmlNodeList elements0 = xml.SelectNodes("//bk:bookstore/book/title", nsmgr);


But I don't know what to do if I have the second example and I also want to use the attribute Author as a variable. I have tried this string au = "Tommy"; XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("bk", "http://www.test.com/"); XmlNodeList elements0 = xml.SelectNodes("//bk:bookstore/book[@author={0}]/title", au, nsmgr); or this XmlNodeList elements0 = xml.SelectNodes("//bk:bookstore/book[@author={0}]/title", nsmgr, au); or this XmlNodeList elements0 = xml.SelectNodes("//bk:bookstore/book[@author={1}]/title", nsmgr, au); but it doesn't work. Could anybody help me?


No comments:

Post a Comment