Topic nodes in your XML use the content namespace - you need to declare and use the XML namespace in your code, then you can use SelectNodes() to capture the nodes of interest - this worked for me
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable); nsmgr.AddNamespace("content", "sitename.xsd"); var topicNodes = xdoc.SelectNodes("//content:Topic", nsmgr); foreach (XmlNode node in topicNodes) { string topic = node.Attributes["TopicName"].Value; }
Like the comparison, see how easy it would be with Linq to XML:
XDocument xdoc = XDocument.Load("test.xml"); XNamespace ns = "sitename.xsd"; string topic = xdoc.Descendants(ns + "Topic") .Select(x => (string)x.Attribute("TopicName")) .FirstOrDefault();
To get all the topics, you can replace the last statement:
var topics = xdoc.Descendants(ns + "Topic") .Select(x => (string)x.Attribute("TopicName")) .ToList();
source share