Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public void insertXML()
  {
      try
      {

          // Open the XML doc
          System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
          myXmlDocument.Load(Server.MapPath("AddProducts.xml"));

          System.Xml.XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

          // Create new XML element and populate its attributes
          System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("product");
          myXmlElement.SetAttribute("name", Server.HtmlEncode(txtname.Text));


          // Insert data into the XML doc and save
          myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
          myXmlDocument.Save(Server.MapPath("AddProducts.xml"));

          // Re-bind data since the doc has been added to
          BindData();
      }
      catch (Exception ex)
      {
          Log(ex.Message, ex.StackTrace);
          ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Exception Message", "alert('You have an exception,please consult IT department')", true);
      }

  }

My XML File is
XML
<?xml version="1.0" encoding="utf-8"?>
<Footballers>
  <product name="iop" />
  <product name="Selvit E" />
  <product name="Selvit E" />
  <product name="Ventrifort A" />
  <product name="Generator" />
  <product name="TEst" />        
  <product name="test" />
  <product name="Test" />
  <product name="test" />
  <product name="ILT" />
  <product name="No Parking" />
  <product name="No Parking" />
  <product name="Livol Liquid" />
  <product name="G-promin" />
</Footballers>



Please how to do this?please help me?
Posted
Updated 24-Aug-14 18:39pm
v2
Comments
Sergey Alexandrovich Kryukov 25-Aug-14 1:02am    
Why adding those duplicates in data in first place? Would not avoiding it be better then removing them later? :-)
—SA

1 solution

Before actually creating a node you can check if there's any duplicate existing using XPathExpression.

C#
// Open the XML doc  
XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load("XMLFile1.xml");

XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;

//Before creating a new element check if an element like that already exists
// If data is case sensitive you can use either String.ToLower() or String.ToUpper()
// to get your data in same case.
string expression = string.Format(@"/Footballers/product[@name = ""{0}""]", Server.HtmlEncode(txtname.Text));
XmlNodeList existingNodes = myXmlDocument.SelectNodes(expression);

// Create new XML element and populate its attributes only if no existing nodes found
if (existingNodes.Count == 0)
{
    XmlElement myXmlElement = myXmlDocument.CreateElement("product");
    myXmlElement.SetAttribute("name", Server.HtmlEncode(txtname.Text));

    // Insert data into the XML doc and save  
    myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
}


Using Xpath expression will allow you a good range of filters to filter your nodes.

Have a look at
XPath Tutorial[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900