XmlDocument oXmlDocument = new XmlDocument();
oXmlDocument.Load(@"D:\VanithaApps\SenMail\DiagramData.xml");
Boolean nodeExits = false;
XmlNode oXmlRootNode = oXmlDocument.SelectSingleNode("records");
XmlNodeList xmlnode = oXmlDocument.GetElementsByTagName("record");
if (delete=="1")
{
if (xmlnode.Count > 0)
{
for (int i = 0; i < xmlnode.Count; i++)
{
string tempVar = element.Substring(0, element.Length - 1);
if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
{
try
{
oXmlRootNode.RemoveChild(xmlnode[i]);
goto Found;
}
catch(Exception ex)
{
ex.ToString();
}
}
}
}
}
if (xmlnode.Count > 0)
{
for (int i = 0; i < xmlnode.Count; i++)
{
string tempVar = element.Substring(0, element.Length-1);
if (xmlnode[i].ChildNodes[2].InnerText == tempVar)
{
nodeExits = true;
XmlNode XAxis = xmlnode[i].ChildNodes[0];
XAxis.InnerText = Convert.ToString(x);
XmlNode YAxis = xmlnode[i].ChildNo开发者_C百科des[1];
YAxis.InnerText = Convert.ToString(y);
}
}
if (nodeExits == false)
{
CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);
}
}
else
{
CreateNewNode(x, y, element, userid, oXmlDocument, oXmlRootNode);
}
Found:
int result = 0;
return result;
I haven't used xml extensively before
<?xml version="1.0" encoding="utf-8"?>
<records>
<record>
<X-Cordinate>774</X-Cordinate>
<Y-Cordinate>173</Y-Cordinate>
<Element>drag595</Element>
<UserID>1</UserID>
</record>
</records>
i want to delete whose child 'Element' value is equal to tempVar.Here if the Element value is equal to drag595 ,i want to remove that entry from my XML .Change
oXmlRootNode.RemoveChild(xmlnode[i]);//not working//
to
xmlnode[i].ParentNode.RemoveChild(xmlnode[i]);
Children can only be removed from their respective parents.
精彩评论