// Open the XML doc
XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load(Server.MapPath("AddProducts.xml"));
XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;
// Create new XML element and populate its attributes
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"));
我的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>
在我的xml文件中存储重复的值。我不想存储重复的值。 我怎样才能做到这一点?
答案 0 :(得分:3)
试试这个:
var doc = XDocument.Load(path)
var elements = doc.Root.Elements().Distinct();
var newDoc = new XDocument(
new XElement("Footballers", elements));
newDoc.Save(newPath);