Solved

reading XML Element

Posted on 2011-09-19
6
395 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36564457
0
 
LVL 7

Author Comment

by:nisarkhan
ID: 36564470
i expect just more than  a link
0
 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 250 total points
ID: 36564886
 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Assisted Solution

by:binaryevo
binaryevo earned 100 total points
ID: 36569923
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
ID: 36571856
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 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 total points
ID: 36595465
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…

696 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