Results 1 to 3 of 3

Thread: help in editing xml in VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Location
    Vermont Republic
    Posts
    11

    help in editing xml in VB

    Okay so I'm trying to learn how to edit .xml whiten visual studio. I've gotten basic knowledge knowledge on .xml, however I don't know how to edit it. Here is an example .xml code that I want to edit. It's basic but a start.

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!-- Edited by XMLSpy -->
    
    <note>
    
       <to>Tove</to>
    
       <from>Jani</from>
    
       <heading>Reminder</heading>
    
       <body>Don't forget me this weekend!</body>
    
    </note>
    Last edited by Tiger151; Oct 22nd, 2014 at 10:27 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: help in editing xml in VB

    The way that I prefer to handle XML files, especially styled like how you have it, is to create a class and serialize it. Take a look at this class:
    Code:
    Public Class Note
    
        Public Property [To] As String
        Public Property From As String
        Public Property Heading As String
        Public Property Body As String
    
        Sub New()
            Me.To = String.Empty
            Me.From = String.Empty
            Me.Heading = String.Empty
            Me.Body = String.Empty
        End Sub
    
    End Class
    You could serialize and deserialize it like this:
    Code:
    Private Sub SerializeNote(ByVal path As String, ByVal note As Note)
        Using sw As IO.StreamWriter = New IO.StreamWriter(path)
            Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(note.GetType)
            x.Serialize(sw, note)
        End Using
    End Sub
    
    Private Function DeserializeNote(ByVal path As String) As Note
        Dim newNote As Note = New Note
    
        Using sr As IO.StreamReader = New IO.StreamReader(path)
            Dim x As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(newNote.GetType)
            newNote = DirectCast(x.Deserialize(sr), Note)
        End Using
    
        Return newNote
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Location
    Vermont Republic
    Posts
    11

    Re: help in editing xml in VB

    what if there is a something like this:
    <root>
    <graphicsPreferences>
    <entry>
    <label> RENDER_PIPELINE </label>
    <activeOption> 1 </activeOption>
    </entry>
    </graphicsPreferences>
    </root>
    If i need to edit the active option,
    would it be the same or would you have to edit thx.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width