Read the xml file.
This function is use for
delete xml node dynamically.
///
<summary>
///
///
</summary>
/// <param
name="path">xml file path</param>
/// <param
name="tagname">root tagname</param>
/// <param
name="searchconditionAttributename">Search
Attribute Name</param>
/// <param
name="searchconditionAttributevalue">Search
Attribute Value</param>
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);
}
This function is use for edit node in xml.
///
<summary>
///
///
</summary>
/// <param
name="path">xml file path</param>
/// <param
name="tagname">root tagname</param>
/// <param
name="searchconditionAttributename">Search
Attribute Name</param>
/// <param
name="searchconditionAttributevalue">Search
Attribute Value</param>
/// <param
name="editAttributename">Edit Attribute Name</param>
/// <param
name="editAttributevalue">Edit Attribute Value</param>
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);
}
I use following .xml file
<?xml
version="1.0"
encoding="utf-8"?>
<users>
<user
/>
<user
name='aaa'
password='111'
status='0'
expireson='2012-12-31'
createdon='2011-05-31'
/>
<user
name='bbb'
password='222'
status='0'
expireson='2012-12-31'
createdon='2011-05-31'/>
<user
name='ccc'
password='333'
status='0'
expireson='2011-12-31'
createdon='2011-05-31'/>
<user
name='ddd'
password='444'
status='0'
expireson='2011-12-31'
createdon='2011-05-31'/>
</users>