Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

reading XML Element

Posted on 2011-09-19
6
Medium Priority
?
408 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
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
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 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 Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 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