Solved

how to remove duplicate elements in XML while loading.

Posted on 2011-09-20
6
1,599 Views
Last Modified: 2013-11-11
below is the code working on loading the xml element in the dropdownlist but my questions is, how can i load only unique data
some of the topicname has a duplicate.
<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="Marketing2" />
            </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="Marketing3" />
            </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
Comment
Question by:nisarkhan
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
JPJ78 earned 350 total points
Comment Utility
Add the DropDownList1.Items.FindByValue() before adding the new listitem.
Adding this prevents the addintion multiple items with the same value.
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();

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


        }

Open in new window

0
 
LVL 7

Author Comment

by:nisarkhan
Comment Utility
Error      1      Operator '!' cannot be applied to operand of type 'System.Web.UI.WebControls.ListItem'       
0
 
LVL 7

Author Comment

by:nisarkhan
Comment Utility

if(DropDownList1.Items.FindByValue(x))
                       DropDownList1.Items.Add(new ListItem(x, x));

Error      1      Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'bool'
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 10

Expert Comment

by:JPJ78
Comment Utility
if(DropDownList1.Items.FindByValue(x) == null)
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 150 total points
Comment Utility
Hi nisarkhan;

Seeming you posted this question into the Linq thread as well here is a Linq solution to the question. Change the LoadXML function as follows:

public void LoadXML( )
{
    XElement xdoc = XElement.Load( "C:/Working Directory/Catalog.xml" );
    XNamespace content = "sitename.xsd";

    var topics = ( from t in xdoc.Descendants( content + "Topic" )
                   select t.Attribute( "TopicName" ).Value ).Distinct( );

    foreach (string topic in topics)
    {
        DropDownList1.Items.Add(topic);                
    }
}

Open in new window


Fernando
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility

Replace the path in this statement :

XElement xdoc = XElement.Load( "C:/Working Directory/Catalog.xml" );

To the path where you have your XML file.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

15 Experts available now in Live!

Get 1:1 Help Now