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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Datta KharadRank #2,135 · 668 reputationPosted Nov 1, 2011, 10:56 PM EDT
// New XML Element CreatedXmlElement newcatalogentry = xmldoc.CreateElement("Order");// New Attribute CreatedXmlAttribute newcatalogattr = xmldoc.CreateAttribute("ID");// Value given for the new attributenewcatalogattr.Value = "005";// Attach the attribute to the XML elementnewcatalogentry.SetAttributeNode(newcatalogattr);// First Element - Order - CreatedXmlElement firstelement = xmldoc.CreateElement("BillNo");// Value given for the first elementfirstelement.InnerText = "1720";// Append the newly created element as a child elementnewcatalogentry.AppendChild(firstelement);// Second Element - BillDate - CreatedXmlElement secondelement = xmldoc.CreateElement("BillDate");// Value given for the second elementsecondelement.InnerText = "1-2-2011";// Append the newly created element as a child elementnewcatalogentry.AppendChild(secondelement);// New XML element inserted into the documentxmldoc.DocumentElement.InsertAfter(newcatalogentry, xmldoc.DocumentElement.LastChild);FileStream fsxml = new FileStream(path,FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);// XML Document Savedxmldoc.Save(fsxml); =============OR========================= //Create the XmlDocument.Hemant KumarRank #1,202 · 1.5K reputationPosted Nov 1, 2011, 11:08 PM EDT
http://www.dotnetspider.com/forum/157958-Using-C-How-Save-Update-Data-from-XML-with-out-using-sql-server-database.aspx
Sam HobbsRank #107 · 20.5K reputationPosted Nov 2, 2011, 11:03 AM EDT
Nafihudheen KT34 reputationPosted Nov 4, 2011, 3:05 AM EDT
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);
}
Datta KharadRank #2,135 · 668 reputationPosted Nov 4, 2011, 3:54 AM EDT
You are welcome...