problem with XmlDocument
-
Hi there I am having problems using the XmlDocument class when editing an xml file. This is what my xml file looks like : Code Project visiontec somefakepwd gr8 website. c# fourms and more. This is the way i am using XmlDocument : private bool SaveXml( string WebsiteName, string UserId, string Pwd, string Description ) { XmlDocument doc = new XmlDocument(); try { doc.Load("vtinfokeeper.xml"); } catch(Exception) { return false; } XmlElement newWebPwd = doc.CreateElement("WebPwd"); XmlElement newWebsite = doc.CreateElement("website"); newWebsite.InnerText = WebsiteName; newWebPwd.AppendChild(newWebsite); XmlElement newUID = doc.CreateElement("uid"); newUID.InnerText = UserId; newWebPwd.AppendChild(newUID); XmlElement newPwd = doc.CreateElement("pwd"); newPwd.InnerText = Pwd; newWebPwd.AppendChild(newPwd); XmlElement newDescr = doc.CreateElement("description"); newDescr.InnerText = Description; newWebPwd.AppendChild(newDescr); doc.DocumentElement.AppendChild(newWebPwd); XmlTextWriter tr = new XmlTextWriter("vtinfokeeper.xml",null); tr.Formatting = Formatting.Indented; try { doc.WriteContentTo(tr); } catch(Exception) { return false; } tr.Close(); return true; } Now when i run the above code i get this result : Code Project visiontec vtlives gr8 website. c# fourms and more. somwwebsite somewuid somwpwd somedescr Whereas i wish to accomplish the result below : Code Project visiontec vtlives gr8 website. c# fourms and more. somwwebsite somewuid somwpwd somedescr What am i doing wrong? Can anyone plz point out the correct way that i should use the XmlDocument class to get my desired result. And as u can
-
Hi there I am having problems using the XmlDocument class when editing an xml file. This is what my xml file looks like : Code Project visiontec somefakepwd gr8 website. c# fourms and more. This is the way i am using XmlDocument : private bool SaveXml( string WebsiteName, string UserId, string Pwd, string Description ) { XmlDocument doc = new XmlDocument(); try { doc.Load("vtinfokeeper.xml"); } catch(Exception) { return false; } XmlElement newWebPwd = doc.CreateElement("WebPwd"); XmlElement newWebsite = doc.CreateElement("website"); newWebsite.InnerText = WebsiteName; newWebPwd.AppendChild(newWebsite); XmlElement newUID = doc.CreateElement("uid"); newUID.InnerText = UserId; newWebPwd.AppendChild(newUID); XmlElement newPwd = doc.CreateElement("pwd"); newPwd.InnerText = Pwd; newWebPwd.AppendChild(newPwd); XmlElement newDescr = doc.CreateElement("description"); newDescr.InnerText = Description; newWebPwd.AppendChild(newDescr); doc.DocumentElement.AppendChild(newWebPwd); XmlTextWriter tr = new XmlTextWriter("vtinfokeeper.xml",null); tr.Formatting = Formatting.Indented; try { doc.WriteContentTo(tr); } catch(Exception) { return false; } tr.Close(); return true; } Now when i run the above code i get this result : Code Project visiontec vtlives gr8 website. c# fourms and more. somwwebsite somewuid somwpwd somedescr Whereas i wish to accomplish the result below : Code Project visiontec vtlives gr8 website. c# fourms and more. somwwebsite somewuid somwpwd somedescr What am i doing wrong? Can anyone plz point out the correct way that i should use the XmlDocument class to get my desired result. And as u can
You loaded the document, then you told the document to create a new node. It doesn't know you want it inside the WebPwds element, so it just adds it to the end of its children (that means it adds it after the WebPwds element). You need to call "AppendChild" or something similar from the XmlNode you want to add to. You could for instance do something like: XmlNode webPwdsNode = doc.FirstChild; webPwds.AppendChild(newPwdNode); where newPwdNode is an XmlNode you created that contains all of the information you want. I personally normally load in a document (I usually validate with a schema too when I load stuff in) and create classes based upon what I read (I do this by hand instead of using serialization). When it's time to write it out, I then create a XmlTextWriter and write out the document from scratch.
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins