See this piece of code I have written for you to remove the Duplicate nodes from XML.Run the code as it. ( I have extended your xml a bit to see proper result)
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXML);
XmlNodeList xNodeList = xDoc.SelectNodes("NewDataSet/ProdSheet");
string[] keyValue = new string[xNodeList.Count];
int count = 0;
foreach (XmlNode xNode in xNodeList)
{
foreach (XmlNode xNodeInner in xNode.ChildNodes)
{
if (xNodeInner.Name == "BaseSku")
{
keyValue[count] = xNodeInner.InnerText;
count++;
//Here we gather all the Values from the element -BaseSku in a string Array.
}
}
}
foreach (string strIfDuplicate in keyValue)
{
XmlNodeList xNodeCheck = xDoc.SelectNodes("NewDataSet/ProdSheet[BaseSku ='"+strIfDuplicate+"']");
if(xNodeCheck.Count >1)
{
for (int i = 1; i < xNodeCheck.Count; i++)
{
xNodeCheck[i].RemoveAll();
}
}
}
string outputXML = xDoc.OuterXml;
Please mark this post as Answer if it is of help to you!
I would love to change the world, but they wont give me the source code.
Marked as answer by SudhaYogesh on Sep 08, 2009 09:50 AM
kavita_khand...
Star
9767 Points
1930 Posts
Re: How to identify duplicate value in XML file using ASP.net (c#)?
Sep 08, 2009 05:06 AM|LINK
See this piece of code I have written for you to remove the Duplicate nodes from XML.Run the code as it. ( I have extended your xml a bit to see proper result)
string myXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"+
"<NewDataSet xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"+
"<ProdSheet>"+
"<ProductId>851</ProductId>"+
"<BaseSku>ACCJVC01</BaseSku>"+
"<Name>JVC GUMY In-Ear Headphone with Colour Cord (Mint Blue)</Name>"+
"</ProdSheet>"+
"<ProdSheet>"+
"<ProductId>852</ProductId>"+
"<BaseSku>ACCJVC01</BaseSku>"+
"<Name>JVC GUMY In-Ear Headphone with Colour Cord (Black)</Name>"+
"</ProdSheet>"+
"<ProdSheet>" +
"<ProductId>852</ProductId>" +
"<BaseSku>ACCJVC01</BaseSku>" +
"<Name>JVC GUMY In-Ear Headphone with Colour Cord (Black)</Name>" +
"</ProdSheet>" +
"<ProdSheet>" +
"<ProductId>852</ProductId>" +
"<BaseSku>ACCJVC01</BaseSku>" +
"<Name>JVC GUMY In-Ear Headphone with Colour Cord (Black)</Name>" +
"</ProdSheet>" +
"<ProdSheet>"+
"<ProductId>853</ProductId>"+
"<BaseSku>ACCJVCHAF130DE</BaseSku>"+
"<Name>JVC GUMY In-Ear Headphone with Colour Cord (Orange)</Name>"+
"</ProdSheet>"+
"</NewDataSet>";
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXML);
XmlNodeList xNodeList = xDoc.SelectNodes("NewDataSet/ProdSheet");
string[] keyValue = new string[xNodeList.Count];
int count = 0;
foreach (XmlNode xNode in xNodeList)
{
foreach (XmlNode xNodeInner in xNode.ChildNodes)
{
if (xNodeInner.Name == "BaseSku")
{
keyValue[count] = xNodeInner.InnerText;
count++;
//Here we gather all the Values from the element -BaseSku in a string Array.
}
}
}
foreach (string strIfDuplicate in keyValue)
{
XmlNodeList xNodeCheck = xDoc.SelectNodes("NewDataSet/ProdSheet[BaseSku ='"+strIfDuplicate+"']");
if(xNodeCheck.Count >1)
{
for (int i = 1; i < xNodeCheck.Count; i++)
{
xNodeCheck[i].RemoveAll();
}
}
}
string outputXML = xDoc.OuterXml;
I would love to change the world, but they wont give me the source code.