Hi all ,
i want to save different user name and password in single XML file .
for this i wrote this code on save button :-
string filename = "E:\\firstXML.xml";
XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("Credential");
xmlWriter.Close();
xmlDoc.Load(filename);
XmlNode root = xmlDoc.DocumentElement;
XmlElement childNode1 = xmlDoc.CreateElement("UserName");
XmlElement childNode2 = xmlDoc.CreateElement("Password");
root.AppendChild(childNode1);
childNode1.InnerText = tbName.Text;
childNode2.InnerText = tbPassword.Text;
xmlDoc.Save(filename);
USING THIS XML IS CREATING BUT NEXT TIME IF I AM INSERTING DIFFERENT USER NAME AND PASSWORD IT IS REPLACING MY PREVIOUS DATA. HOW TO ADD ALL USER NAME AND PASSWORD INTO SINGLE XML FILE PLEASE HELP..

Dorababu MekaRank #457 · 5.5K reputationPosted Dec 29, 2012, 2:58 AM EST
you can do this way
doc = new XDocument(
new XElement("ROOT",
new XElement("Cities",
new XElement("City",
new XAttribute("id", 1),
new XElement("CityName", "Hyd"))), new XElement("Countries",
new XElement("Country",
new XAttribute("id", 1),
new XElement("Countryname", "India")))));
and when updating you can do this
Dorababu MekaRank #457 · 5.5K reputationPosted Dec 28, 2012, 11:46 PM EST
string file = "your path";
XDocument doc = XDocument.Load(file);
XElement ele = new XElement("you required",tbName.Text);
doc.Root.Add(ele); // will add as root
doc.Save(file);
Dorababu MekaRank #457 · 5.5K reputationPosted Dec 29, 2012, 12:34 AM EST
string strPath = Server.MapPath("Demo.xml");
XDocument doc;
if (!System.IO.File.Exists(strPath))
{
doc = new XDocument(
new XElement("Cities",
new XElement("City",
new XAttribute("id", 1),
new XElement("CityName", "Hyd"))));
doc.Save(strPath);
}
else
{
doc = XDocument.Load(strPath);
XElement ele = new XElement("City",
new XAttribute("id", 3),
new XElement("CityName", "Vizag"));
doc.Root.Add(ele);
doc.Save(strPath);
}
Check this sample
http://blogs.microsoft.co.il/blogs/maordavid/archive/2007/10/13/linq-to-xml-adding-updating-and-deleting-data.aspx
Sumit KumawatRank #4,008 · 372 reputationPosted Dec 29, 2012, 1:46 AM EST
thanks for your reply it is working as i wanted. i want to know one more thing i want to add some more Xelement in my Xdocument so i wrote like this :-
doc = new XDocument(
new XElement("Cities",
new XElement("City",
new XAttribute("id", 1),
new XElement("CityName", "Hyd"))),
new XElement("Countries",
new XElement("Country",
new XAttribute("id", 1),
new XElement("Countryname", "India"))));
but this is giving error
"This operation would create an incorrectly structured document."
plz tell me how to add another xelement and also how to read this using C#
plz share some link .
thanks
Dorababu MekaRank #457 · 5.5K reputationPosted Dec 29, 2012, 2:52 AM EST
Is this you are trying for
doc = new XDocument(
new XElement("Cities",
new XElement("City",
new XAttribute("id", 1),
new XElement("CityName", "Hyd")), new XElement("Countries",
new XElement("Country",
new XAttribute("id", 1),
new XElement("Countryname", "India")))));
Sumit KumawatRank #4,008 · 372 reputationPosted Dec 30, 2012, 8:03 PM EST
please tell me how to read below XML using C#