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

x
?
Solved

Loading XML data in Dropdownlist

Posted on 2011-09-19
3
Medium Priority
?
331 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
[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
3 Comments
 
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 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

660 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