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 MekaPosted Dec 29, 2012, 7:58 AM
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
Sumit KumawatPosted Dec 31, 2012, 1:03 AM
please tell me how to read below XML using C#
Dorababu MekaPosted Dec 29, 2012, 7:52 AM
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 KumawatPosted Dec 29, 2012, 6:46 AM
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 MekaPosted Dec 29, 2012, 5:34 AM
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
Dorababu MekaPosted Dec 29, 2012, 4:46 AM
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);