Solved

reading XML Element

Posted on 2011-09-19
6
396 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
Stressed Out?

Watch some penguins on the livecam!

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

734 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