Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading XML Element

Posted on 2011-09-19
6
Medium Priority
?
404 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 1000 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
Technology Partners: 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 400 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 600 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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