Click here to Skip to main content
15,741,818 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I am trying to store data to an XML file but I cannot get it to store the way I want. I am sure the problem is something very obvious and easy to fix but I dont have much experience with XML so it is confusing me. Basically I am using the routine below (at bottom) to open an existing file from within isolated storage, change a value corresponding to a tag of interest and save the file. The XML starts off like:

XML
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 1</Test1>
</UserSettings>


and should end up like

XML
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 2</Test1>
</UserSettings>


but instead ends up like

XML
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 1</Test1>
</UserSettings><?xml version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 2</Test1>
</UserSettings>


The code I use to save is

VB
Public Sub EditSettingsFile(ByVal sTag As String, ByVal sValue As String)

        Dim stmWriter As New IsolatedStorageFileStream("PlanningGDBToolsSettings.xml", FileMode.Open, m_pIsoStorage)
        Dim writer As New Xml.XmlTextWriter(stmWriter, Encoding.UTF8)

        Dim xmldoc As New Xml.XmlDataDocument()
        Dim xmlnode As Xml.XmlNodeList
        Dim i As Integer

        Try
            xmldoc.Load(stmWriter)
            xmlnode = xmldoc.GetElementsByTagName(sTag)
            For i = 0 To xmlnode.Count - 1
                If xmlnode(i).Name.Trim = sTag.Trim Then
                    xmlnode(i).InnerText = sValue.Trim
                End If
            Next

            writer.Formatting = Xml.Formatting.Indented
            xmldoc.WriteContentTo(writer)

        Catch ex As Exception

            MsgBox("OpenSettingsFile - " & ex.Message)

        Finally

            writer.Flush()
            writer.Close()
            stmWriter.Close()

        End Try

    End Sub


Can anyone please let me know where I am going wrong.

Thanks in advance
Posted

In every way :-) Use XPath to select elements, don't iterate over all of them. I don't see how this code could create the document you're showing us though, it's not even XML, so it would not fit in an XmlDataDocument. I think WriteContentTo is writing the document AFTER the current one. Use XmlDocument and the Load and Save methods.
 
Share this answer
 
Damn I suspected as much! This is code I was using to edit, the code I used to create it is below. Basically I check to see if the file exists and if not I run the create then edit, if it does then I run just the previous edit sub. Thanks for the reply, I will look into XPath and let you know how I go :-)

VB
Public Sub CreateSettingsFile()

       Dim stmWriter As New IsolatedStorageFileStream("PlanningGDBToolsSettings.xml", FileMode.Create, m_pIsoStorage)
       Dim writer As New Xml.XmlTextWriter(stmWriter, Encoding.UTF8)

       Try
           writer.Formatting = Xml.Formatting.Indented
           writer.WriteStartDocument()
           writer.WriteStartElement("UserSettings")

           writer.WriteStartElement("WorkingMXD")
           writer.WriteString("Some mxd")
           writer.WriteEndElement()

           writer.WriteStartElement("Test1")
           writer.WriteString("Test unsuccessful")
           writer.WriteEndElement()

           writer.WriteEndElement()

       Catch ex As Exception

           MsgBox("CreateSettingsFile - " & ex.Message)

       Finally

           writer.Flush()
           writer.Close()
           stmWriter.Close()

       End Try
   End Sub
 
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