Solved

Loading XML data in Dropdownlist

Posted on 2011-09-19
3
321 Views
Last Modified: 2013-12-17
Hi,

I have a XML file that I want to load all the child elements(TopicName) into a Dropdownlist.

Thanks
<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 Corporation1" />
        <content:Topics Scheme="ABC">
          <content:Topic TopicId="1" TopicName="Marketing1" />
          <content:Topic TopicId="11" TopiccName="Coverage1" />
        </content:Topics>
      </ContentItem>
    </CatalogItem>
  </CatalogItems>
  <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 Corporation2" />
      <content:Topics Scheme="ABC">
        <content:Topic TopicId="2" TopicName="Marketing2" />
        <content:Topic TopicId="22" TopiccName="Coverage2" />
      </content:Topics>
    </ContentItem>
  </CatalogItem>
  </CatalogItems>
  <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 Corporation3" />
      <content:Topics Scheme="ABC">
        <content:Topic TopicId="3" TopicName="Marketing3" />
        <content:Topic TopicId="33" TopiccName="Coverage3" />
      </content:Topics>
    </ContentItem>
  </CatalogItem>
  </CatalogItems>
</content>

Open in new window

0
Comment
Question by:nisarkhan
3 Comments
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 36564477
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36564865
You can use xmldatasource.

DropDownList ID="DropDownList1" runat="server" DataSourceID="myXML" DataTextField="TopicName" DataValueField="TopicId" />
 
<asp:XmlDataSource ID="myXML" runat="server" DataFile="~/myXMLFile.xml"  XPath="xpath of your element" />
0
 
LVL 16

Accepted Solution

by:
disrupt earned 500 total points
ID: 36564912
1) You need to fix that XML document I edited that for you
2) Use the code below I fixed for you, I tested it and it works

In ASPX:

<asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList>

New XML:

<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 Corporation1" />
                <content:Topics Scheme="ABC">
                    <content:Topic TopicId="1" TopicName="Marketing1" />
                    <content:Topic TopicId="11" TopicName="Coverage1" />
                </content:Topics>
            </ContentItem>
        </CatalogItem>
    <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 Corporation2" />
            <content:Topics Scheme="ABC">
                <content:Topic TopicId="2" TopicName="Marketing2" />
                <content:Topic TopicId="22" TopicName="Coverage2" />
            </content:Topics>
        </ContentItem>
    </CatalogItem>
    <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 Corporation3" />
            <content:Topics Scheme="ABC">
                <content:Topic TopicId="3" TopicName="Marketing3" />
                <content:Topic TopicId="33" TopicName="Coverage3" />
            </content:Topics>
        </ContentItem>
    </CatalogItem>
    </CatalogItems>
</content>
protected void Page_Load(object sender, EventArgs e)
        {
            LoadXML();
        }

        public void LoadXML()
        {
               
            XmlDocument doc = new XmlDocument();
            doc.Load("PATH TO XML HERE");
            XmlNodeList elemList = doc.GetElementsByTagName("content:Topic");

                for (int i = 0; i < elemList.Count; i++)
                {
                    string x = elemList[i].Attributes["TopicName"].Value.ToString();

                    DropDownList1.Items.Add(new ListItem(x, x));
                }  


        }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

23 Experts available now in Live!

Get 1:1 Help Now