Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends i have already created xml file in appdata inside vs 2010

it working fine but it is not saving it showing access denied it is using by another person

my code is
VB
If (IO.File.Exists(Server.MapPath("~/App_Data/XMLFile1.xml"))) Then

            Dim document As XmlReader = New XmlTextReader(Server.MapPath("~/App_Data/XMLFile1.xml"))

            While (document.Read())

                Dim type = document.NodeType


                If (type = XmlNodeType.Element) Then

                    If document.Name <> dpFiles.SelectedItem.Text Then

                        Dim innertext As String
                        innertext = document.ReadInnerXml.ToString()

                        Dim filename As String = Server.MapPath("~/App_Data/XMLFile1.xml")

                        'create new instance of XmlDocument
                        Dim doc As New XmlDocument()

                        'load from file
                        doc.Load(filename)
                        Try

                            Dim node As XmlNode = doc.CreateNode(XmlNodeType.Element, "" & dpFiles.SelectedItem.Text & "", Nothing)
                            'node.InnerText = "this is new node";

                            'create title node
                            Dim nodeTitle As XmlNode = doc.CreateElement("" & dpFiles.SelectedItem.Text & "")
                            'add value for it
                            nodeTitle.InnerText = lblRandomNumber.Text

                            'create FileName
                            Dim nodeFileName As XmlNode = doc.CreateElement("FileName")
                            nodeFileName.InnerText = txtFileName.Text

                            'create FilePath
                            Dim nodeFilePath As XmlNode = doc.CreateElement("FilePath")
                            nodeFilePath.InnerText = txtFolderPath.Text

                            'create FileDesc
                            Dim nodeFileDesc As XmlNode = doc.CreateElement("Description")
                            nodeFileDesc.InnerText = txtDesc.Text

                            'add to parent node
                            node.AppendChild(nodeTitle)
                            node.AppendChild(nodeFileName)
                            node.AppendChild(nodeFilePath)
                            node.AppendChild(nodeFileDesc)

                            'add to elements collection
                            doc.DocumentElement.AppendChild(node)

                            'save back
                            doc.Save(filename)
                        Catch ex As Exception
                            MsgBox(ex.ToString())
                        End Try
                    End If
                End If
            End While
Posted
Updated 23-Jun-13 19:17pm
v2

1 solution

Yes, and the other process accessing the file is also your process.
You open the file at least twice:
VB
Dim document As XmlReader = New XmlTextReader(Server.MapPath("~/App_Data/XMLFile1.xml"))

and then several times in a loop - well, likely once only because of the failure:
VB
Dim filename As String = Server.MapPath("~/App_Data/XMLFile1.xml")
Dim doc As New XmlDocument()
doc.Load(filename)

Find out what you want to do. And then structure the code accordingly. I cannot believe that you want to add data to that file while you are reading it - rather do that afterwards.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900