Solved

Loading XML data in Dropdownlist

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.net VBA word safemode 1 24
JavaScript error 1 34
Vb.net/SQL prevent a table from being deleted ever 7 23
Entity Framework 3 14
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

12 Experts available now in Live!

Get 1:1 Help Now