Been looking around for an answer and cant find anything - Im quite new so maybe Im not hitting up the right key words?
This is a sample of the XML I am working with
<database>
<book>
<title>A</title>
<author>
<name>1</name>
</author>
</book>
<book>
<title>B</title>
<author>
<name>2</name>
</author>
<author>
<name>3</name>
</author>
<author>
<name>4</name>
</author>
<author>
<name>5</name>
</author>
</book>
</database>
Im trying to use C# XMLDocument to grab for book A author 1 and then for book B author 1, 2, 3, 4, 5
So far the code I am working with is cycling through ALL the authors so I am getting book A author 1, 2, 3, 4, 5
The code I have so far is as follows roughly
XmlDocument doc = new XmlDocument();
doc.Load("myxmlfile");
XmlNode root = doc.SelectSingleNode("database");
XmlNodeList nodelist = root.SelectNodes("book");
foreach (XmlNode n in nodelist)
{
XmlNodeList authors = root.SelectNodes(".//author");
book.authorstring = "";
foreach (XmlNode author in authors)
{
book.authorstring = book.authorstring+author.SelectSingleNode("name").InnerText + ", ";
}
}
I read some where that if I use " . " before the " // " that it would "anchor" to the current node but it doesnt seem to be working and is cycling through all the nodes
What am I doing wrong or missing?
If i understand you correctly your error is in this line :
XmlNodeList authors = root.SelectNodes(".//author");
it should be
XmlNodeList authors = n.SelectNodes(".//author");