Writing to xml files

Pauly Man

Member
Joined
Sep 2, 2003
Messages
9
Hi guys,

I new to these forums, and to xml. I have read most of the posts on xml here, and they have helped a lot. I can now access the data in the xml file that I wrote for a program I have. The xml data is given below:

Code:
<?xml version="1.0" encoding="utf-8" ?> 
- <CADataList xmlns="http://tempuri.org/CAData.xsd">
- <CAData>
- <Rule>
  <Binary>01101110</Binary> 
  <Decimal>110</Decimal> 
  </Rule>
- <AutomatonData>
  <Cell>320</Cell> 
  <Generations>240</Generations> 
  <InitialSeed>0</InitialSeed> 
  <InitialPos>160</InitialPos> 
  </AutomatonData>
  </CAData>
  </CADataList>

I have a schema set up for the xml file, so first question, do i have to keep the schema with the xml file, or is there a way to make the above xml file look in a particular place for the shcema?

The code below is what I use to access the data in the xml file:

Code:
Imports System
Imports System.Xml

Module ModXML

    Public Const filename As String = "CADataList.xml"
    Public binstring As String

    Public Sub XMLRead()

        Dim reader As XmlTextReader = Nothing
        reader = New XmlTextReader(filename)
        reader.WhitespaceHandling = WhitespaceHandling.None

        While reader.Read()
            Select Case reader.NodeType
                Case XmlNodeType.Element
                Case XmlNodeType.Text
                    If reader.Depth = 4 Then
                        Select Case reader.LineNumber
                            Case 5 (The vlaue of "Binary")
                                binstring = reader.Value
                            Case 6 (The vlaue of "Decimal")
                                DecRule = reader.Value
                            Case 9 (The vlaue of "Cell")
                                Cell = reader.Value
                            Case 10 (The vlaue of "Generations")
                                Generations = reader.Value
                            Case 11 (The vlaue of "InitialSeed")
                                InitialSeed = reader.Value
                            Case 12 (The vlaue of "InitialPos")
                                InitialPos = reader.Value
                        End Select
                    End If
                Case XmlNodeType.CDATA
                Case XmlNodeType.ProcessingInstruction
                Case XmlNodeType.Comment
                Case XmlNodeType.XmlDeclaration
                Case XmlNodeType.Document
                Case XmlNodeType.DocumentType
                Case XmlNodeType.EntityReference
                Case XmlNodeType.EndElement
            End Select
        End While

        If Not (reader Is Nothing) Then
            reader.Close()
        End If

        For i = 1 To 8
            Rule(i) = Mid(binstring, i, 1)
        Next i

    End Sub

    Public Sub XMLWrite()

        Dim writer As XmlTextWriter = Nothing

    End Sub


End Module

Cell, Generations, InitialSeed, InitialPos, DecRule, and Rule() are all defined globally in another module.

Basically the xml is holding data for my program, and that data is loaded when the program starts up. The above may not be what you call pretty, or very effective, but it does work for my xml file. (Baby steps at the moment). Now what I want to do is to update the values in the xml file when the program is shutting down, so that next time it starts up it has new initial input. Is there a way I can simply write to a specific node of the xml file? From what Ive read xmlwriter simply writes over the xml file completely, is this right?
 
Back
Top