Solved

Loading XML data in Dropdownlist

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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