I need to delele the user tag (name as 'aaa') also
I need to edit the expireson on the user tag(name as 'ddd')
I'm new to c# language. I dono how to do this.
My sample Code for delete:
XmlDocument doc = new XmlDocument();
doc.Load(sample.xml");
XmlElement elmRoot = doc.DocumentElement;
string xPath = "/users/user[@name='aaa']";
XmlNode findnode = elmRoot.SelectSingleNode(xPath);
elmRoot.RemoveAll();
doc.Save(sample.xml");
But not working.
For editing i don't have any idea.
Please any help me.
VulpesPosted Nov 29, 2012, 5:08 AM
Gohil JayendrasinhPosted Nov 29, 2012, 2:23 AM
///
///
///
/// xml file path
/// root tagname
/// Search Attribute Name
/// Search Attribute Value
private static void DeleteXmlNode(string path, string tagname, string searchconditionAttributename, string searchconditionAttributevalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.GetElementsByTagName(tagname);
//XmlNodeList nodes = doc.GetElementsByTagName("user");
foreach (XmlNode node in nodes)
{
foreach (XmlAttribute attribute in node.Attributes)
{
if ((attribute.Name == searchconditionAttributename) && (attribute.Value == searchconditionAttributevalue))
//if ((attribute.Name == "name") && (attribute.Value == "aaa"))
{
//delete.
node.RemoveAll();
break;
}
}
}
//save xml file.
doc.Save(path);
}
///
///
///
/// xml file path
/// root tagname
/// Search Attribute Name
/// Search Attribute Value
/// Edit Attribute Name
/// Edit Attribute Value
private static void EditXmlNode(string path, string tagname, string searchconditionAttributename, string searchconditionAttributevalue, string editAttributename, string editAttributevalue)
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodes = doc.GetElementsByTagName(tagname);
// XmlNodeList nodes = doc.GetElementsByTagName("user");
foreach (XmlNode node in nodes)
{
bool newnode = true;
//Check attribute eixt which we want to edit.
bool isEdit = false;
foreach (XmlAttribute attribute in node.Attributes)
{
//if ((attribute.Name == "name") && (attribute.Value == "ddd"))
if ((attribute.Name == searchconditionAttributename) && (attribute.Value == searchconditionAttributevalue))
{
isEdit = true;
}
if (isEdit)
{
// if (attribute.Name == "expireson")
if (attribute.Name == editAttributename)
{
//attribute.Value = "2011-12-28";
attribute.Value = editAttributevalue;
break;
}
}
}
newnode = false;
}
//save xml file.
doc.Save(path);
}