• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1779
  • Last Modified:

how to remove duplicate elements in XML while loading.

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
nisarkhan
Asked:
nisarkhan
  • 2
  • 2
  • 2
2 Solutions
 
JPJ78Commented:
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
 
nisarkhanAuthor Commented:
Error      1      Operator '!' cannot be applied to operand of type 'System.Web.UI.WebControls.ListItem'       
0
 
nisarkhanAuthor Commented:

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
JPJ78Commented:
if(DropDownList1.Items.FindByValue(x) == null)
0
 
Fernando SotoCommented:
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
 
Fernando SotoCommented:

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now