I store my data in xml file. Everytime I get a data to write I open the xml file, write data to it and then close the xml file. But everytime I write a data to the file, old content is cleared. (I use xmltextwriter.) Can anybody help me to solve this problem?
I also wonder if it is possible to change a text in an xml file! if my xml file is like below how can I change day as 06? 05 09 2003 Thanks a lot...
The easiest way to do this is using XmlDocument instead, though that means you will load/save the whole document each time. For example to change that value, you can just do (untested code): XmlDocument document = new XmlDocument(); document.load("your.xml");
document.selectSingleNode("/data/day").InnerText = "06"; document.Save("your.xml");
ok I have an exact situation as this person. I have an user.xml file with has the following formath 35675E68F4B5AF7B995D9205AD0FC43842F16450 What I want to do is add a new user as the following.... hashpassword So I use the following code Public Function WritePassword(ByVal
user As String, ByVal password As String) Dim applicationpath As String = Request.PhysicalApplicationPath Dim XMLdocpath As String = applicationpath & "/unsecure/users.xml" Dim myxmldocument As New XmlDocument myxmldocument.Load(XMLdocpath) myxmldocument.SelectSingleNode("/data/yaheya").InnerText
= "06" myxmldocument.Save(XMLdocpath) errormessage.InnerHtml = "document saved" End Function Sub Hashpassword(ByVal sender As Object, ByVal e As EventArgs) Dim username As String = txtusername.Text Dim password As String = txtpassword.Text WritePassword(username,
password) End Sub and my aspx page... I get an error object reference not set to an instance of an object...
Yaheya Quazi
Director, Admin. Comp. & Systems
University of California, Merced
What if I want to add a new node? what will be the syntax? And what about the error object reference not set to an object....should it give me an xml error instead?
Yaheya Quazi
Director, Admin. Comp. & Systems
University of California, Merced
The error is correct, since you are calling selectSingleNode with a non-existing node, it will return Null and then you try to call a method on that, so it throws that error. Safer syntax would be:
Dim node As XmlNode
node = doc.selectSingleNode("/xxxx/xxx")
if (node Is Nothing=false) Then
node.InnerText = "06"
End If
The syntax to add XML is quite simple:
Dim element As XmlElement
element = doc.createElement("MyElement")
element.setAttribute("myAttribute", "Some Value")
element.InnerText="Some inner Text"
node.appendChild(element)
' WHERE node is whatever node you want to add the element to
obviously my XML knowledge is really poor :( ok lets say I Want to add this to the end of my xmlfile sha1password How will I use your code? Also I get Access to the path "W:\Inetpub\wwwroot\app1\unsecure\users.xml" is denied I have IUSR_machinename right access
to the folder and the file. What else I need to do?
Yaheya Quazi
Director, Admin. Comp. & Systems
University of California, Merced
ok got it to work for people who may need to see the code ... Public Function WritePassword(ByVal user As String, ByVal password As String) Dim applicationpath As String = Request.PhysicalApplicationPath Dim XMLdocpath As String = applicationpath & "/unsecure/users.xml"
Dim myxmldocument As New XmlDocument myxmldocument.Load(XMLdocpath) Dim xmlElem As System.Xml.XmlNode xmlElem = myxmldocument.CreateNode(System.Xml.XmlNodeType.Element, user, Nothing) xmlElem.InnerXml = password myxmldocument.DocumentElement.AppendChild(xmlElem)
myxmldocument.Save(XMLdocpath) errormessage.InnerHtml = "document saved" End Function
Yaheya Quazi
Director, Admin. Comp. & Systems
University of California, Merced
xelnagal
Member
20 Points
4 Posts
how to append a text in an xml file?
Sep 05, 2003 01:10 PM|LINK
CarlosAg
Participant
1355 Points
271 Posts
Microsoft
Re: how to append a text in an xml file?
Sep 05, 2003 08:33 PM|LINK
xelnagal
Member
20 Points
4 Posts
Re: how to append a text in an xml file?
Sep 09, 2003 01:23 PM|LINK
yaheya
Participant
1635 Points
340 Posts
Re: how to append a text in an xml file?
Sep 12, 2003 09:51 PM|LINK
Director, Admin. Comp. & Systems
University of California, Merced
CarlosAg
Participant
1355 Points
271 Posts
Microsoft
Re: how to append a text in an xml file?
Sep 12, 2003 09:54 PM|LINK
yaheya
Participant
1635 Points
340 Posts
Re: how to append a text in an xml file?
Sep 12, 2003 09:55 PM|LINK
Director, Admin. Comp. & Systems
University of California, Merced
CarlosAg
Participant
1355 Points
271 Posts
Microsoft
Re: how to append a text in an xml file?
Sep 12, 2003 10:00 PM|LINK
Dim node As XmlNode node = doc.selectSingleNode("/xxxx/xxx") if (node Is Nothing=false) Then node.InnerText = "06" End IfThe syntax to add XML is quite simple:Dim element As XmlElement element = doc.createElement("MyElement") element.setAttribute("myAttribute", "Some Value") element.InnerText="Some inner Text" node.appendChild(element) ' WHERE node is whatever node you want to add the element toyaheya
Participant
1635 Points
340 Posts
Re: how to append a text in an xml file?
Sep 12, 2003 10:06 PM|LINK
Director, Admin. Comp. & Systems
University of California, Merced
yaheya
Participant
1635 Points
340 Posts
Re: how to append a text in an xml file?
Sep 12, 2003 11:37 PM|LINK
Director, Admin. Comp. & Systems
University of California, Merced