Is any body know the C# code to create, select , and update the above xml file model please help me.
Thanking you,
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Nov 2, 2011, 2:56 AM
Datta KharadPosted Nov 4, 2011, 7:54 AM
You are welcome...
Nafihudheen KTPosted Nov 4, 2011, 7:05 AM
thanks Mr.Datta Kharad for your replay
here is the code that I used to create and update the XML file
XmlDocument xmlDoc = new XmlDocument();
string PATH = @"C:\mySamleXml.xml";
//if file path exist then update else create
if (System.IO.File.Exists(PATH))
{
//if the file already exist in tha PATH then updates the file
XmlTextReader reader = new XmlTextReader(PATH);
//XmlDocument doc = new XmlDocument();
xmlDoc.Load(reader);
reader.Close();
XmlNode oldNode;
XmlElement root = xmlDoc.DocumentElement;
oldNode = root.SelectSingleNode("/MyFirstXML/Node1");
XmlElement newNode = xmlDoc.CreateElement("Node1");
newNode.SetAttribute("Value", "yourAttributeValue");
newNode.InnerXml = "
newNode["Childnode1"].SetAttribute("Value", "myChildnode1");
newNode["Childnode2"].SetAttribute("Value", "myChildNode2");
root.ReplaceChild(newNode, oldNode);
XmlNode oldNode2;//here is the second node
oldNode2 = root.SelectSingleNode("/MyFirstXML/Node2");
XmlElement newNode2 = xmlDoc.CreateElement("Node2");
newNode2.SetAttribute("Value", "yourNode2Attribute");
root.ReplaceChild(newNode2, oldNode2);
xmlDoc.Save(PATH);//save updated xml nodes
}
else
{
//Creating the xml file if it does not exit in PATH
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("MyFirstXML");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
//create the first node
XmlElement newNode = xmlDoc.CreateElement("Node1");
newNode.SetAttribute("Value", "yourAttributeValue");
xmlDoc.DocumentElement.PrependChild(newNode);
newNode.SetAttribute("Value", "yourAttributeValue");
newNode.InnerXml = "
newNode["Childnode1"].SetAttribute("Value", "myChildnode1");
newNode["Childnode2"].SetAttribute("Value", "myChildNode2");
XmlElement newNode2 = xmlDoc.CreateElement("Node2");
newNode2.SetAttribute("Value", "yourNode2Attribute");
xmlDoc.DocumentElement.PrependChild(newNode2);
xmlDoc.Save(PATH);
}
Sam HobbsPosted Nov 2, 2011, 3:03 PM
Hemant KumarPosted Nov 2, 2011, 3:08 AM
http://www.dotnetspider.com/forum/157958-Using-C-How-Save-Update-Data-from-XML-with-out-using-sql-server-database.aspx