Solved

Loading XML data in Dropdownlist

Posted on 2011-09-19
3
324 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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