Reading node from XML file in XMLDocument

I am trying to capture TopicName , how should I go after it and try another combination, but somehow I can not get TopicName below, this is my source code ...

 XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing xdoc.Load( "http://latestpackagingnews.blogspot.com/feeds/posts/default" );//loading XML in xml doc XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("content");//reading node so that we can traverse thorugh the XML foreach (XmlNode xNode in xNodelst)//traversing XML { //litFeed.Text += "read"; } 

sample xml file

 <content type="application/xml"> <CatalogItems xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="sitename.xsd"> <CatalogSource Acronym="ABC" OrganizationName="ABC Corporation" /> <CatalogItem Id="3212" CatalogUrl="urlname"> <ContentItem xmlns:content="sitename.xsd" TargetUrl="url"> <content:SelectionSpec ClassList="" ElementList="" /> <content:Language Value="eng" Scheme="ISO 639-2" /> <content:Source Acronym="ABC" OrganizationName="ABC Corporation" /> <content:Topics Scheme="ABC"> <content:Topic TopicName="Marketing" /> <content:Topic TopiccName="Coverage" /> </content:Topics> </ContentItem> </CatalogItem> </CatalogItems> </content> 
+4
source share
3 answers

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(); 
+8
source

If you only need a specific item, I would use XPath:

This is a guide to using XPath in C #: http://www.codeproject.com/KB/XML/usingXPathNavigator.aspx

And this is a request that will provide you with a collection of your topics:

 //content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic 

You can customize this query depending on what you are trying to execute, capturing only a specific topic value:

 //content/CatalogItems/CatalogItem/ContentItem/content:Topics/content:Topic/@TopicName 

XPath is pretty easy to learn. I did such things pretty quickly without any prior knowledge.

You can insert an XML and xpath request here to validate your requests:

http://www.bit-101.com/xpath/

0
source

The following quick and dirty LINQ to XML code gets your topic names and prints them to the console.

 XDocument lDoc = XDocument.Load(lXmlDocUri); foreach (var lElement in lDoc.Element("content").Element(XName.Get("CatalogItems", "sitename.xsd")).Elements(XName.Get("CatalogItem", "sitename.xsd"))) { foreach (var lContentTopic in lElement.Element(XName.Get("ContentItem", "sitename.xsd")).Element(XName.Get("Topics", "sitename.xsd")).Elements(XName.Get("Topic", "sitename.xsd"))) { string lTitle = lContentTopic.Attribute("TopicName").Value; Console.WriteLine(lTitle); } } 

This would be much shorter if not all the namespaces :) (Instead of "XName.Get" you would just use the name of the element).

0
source

All Articles