Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML VB.NET
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 version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 1</Test1>
</UserSettings>
 
and should end up like
 
<?xml version="1.0" encoding="utf-8"?>
<UserSettings>
  <WorkingMXD />
  <Test1>Value 2</Test1>
</UserSettings>
 
but instead ends up like
 
<?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
 
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 31-Jul-12 19:46pm
FFP@GC416
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

In every way Smile | :) 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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 Smile | :)
 
 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
 
  Permalink  

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

  Print Answers RSS
0 Maciej Los 245
1 OriginalGriff 210
2 Manfred R. Bihy 190
3 CHill60 180
4 _Amy 155
0 OriginalGriff 7,445
1 Sergey Alexandrovich Kryukov 6,157
2 Maciej Los 3,774
3 Peter Leow 3,448
4 CHill60 2,702


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 1 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100