Solved

reading XML Element

Posted on 2011-09-19
6
389 Views
Last Modified: 2013-12-17


 i am trying to load the TopicName and Title from the below XML what i am missing?
below is my code.
i am trying to grab the `TopicName` how should i go after it and try different combination but somehow i am unable to get `TopicName` below is my source codee...

    XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing
    xdoc.Load(url);
    XmlNodeList xNodelst = xdoc.DocumentElement.SelectNodes("content");//reading node so that we can traverse thorugh the XML
    
    foreach (XmlNode xNode in xNodelst)        
    {
      //
    }

 
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>

Open in new window

0
Comment
Question by:nisarkhan
6 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
Comment Utility
0
 
LVL 7

Author Comment

by:nisarkhan
Comment Utility
i expect just more than  a link
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 250 total points
Comment Utility
 Refer below snippet:
             
                       string sContent = File.ReadAllText(xmlfile);
                        StringReader DocumentReader = new StringReader(sContent);
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load(DocumentReader);
                        ReferenceNodes = xmlDoc.GetElementsByTagName("content:Topic");
                       
                        foreach (XmlNode Node in ReferenceNodes)
                        {
                            if (Node.Attributes["TopicName"] != null)
                            {
                                string sTopicName = Node.Attributes["TopicName"].Value;
                            }
                        }
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 100 total points
Comment Utility
If you like LINQ here is another bit of code you can play with:


 
string filePath = HttpContext.Current.Server.MapPath(SettingsModel.XMLFileLocation + "MyXML.xml");
            if (File.Exists(filePath))
            {
                var xml = (from x in XElement.Load(filePath).Elements("MyXML") 
                           select new XMLModel
                           {
                               ID = x.Element("ID").Value,
                               Value = x.Element("Value").Value
                           });
                return xml;
            }

Open in new window

0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
Comment Utility
Below code returns Topic names.
using System;
using System.IO;
using System.Xml.Linq;
using System.Xml;
using System.Collections;

namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlfile = @"C:\Users\easwaranp\Desktop\test.xml";
            ArrayList arrResult = new ArrayList();
            string sContent = File.ReadAllText(xmlfile);
            StringReader DocumentReader = new StringReader(sContent);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(DocumentReader);
            XmlNodeList ReferenceNodes = xmlDoc.GetElementsByTagName("content:Topic");

            foreach (XmlNode Node in ReferenceNodes)
            {
                if (Node.Attributes["TopicName"] != null)
                {
                    string sTopicName = Node.Attributes["TopicName"].Value;
                    arrResult.Add(sTopicName);
                    
                }
            }

            foreach (string s in arrResult)
            {
                Console.WriteLine(s);

            }
            Console.Read();

        }
    }
}

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 total points
Comment Utility
Hi nisarkhan;

The following Linq to XML quesry will do what you need.

public void LoadXML( )
{
    XElement xdoc = XElement.Load( "C:/Working Directory/Catalog.xml" );
    XNamespace content = "sitename.xsd";

    var topics = ( from t in xdoc.Descendants( content + "Topic" )
                   select t.Attribute( "TopicName" ).Value ).Distinct( );

    foreach (string topic in topics)
    {
        DropDownList1.Items.Add(topic);                
    }
}

Open in new window


Fernando
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now