Click here to Skip to main content
15,886,106 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to I acquire read-write codes for XML in VB.NET?
So I want to read-write XML functions in VB.NET.

Like this:
[^]

But I want to adding for each using command.
(So: XML.Text = XML.Text + [Text])

What I have tried:

I want to like this codes for XML:

VB
Public Class ini
    ' API functions
    Private Declare Ansi Function GetPrivateProfileString _
      Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
      (ByVal lpApplicationName As String,
      ByVal lpKeyName As String, ByVal lpDefault As String,
      ByVal lpReturnedString As System.Text.StringBuilder,
      ByVal nSize As Integer, ByVal lpFileName As String) _
      As Integer
    Private Declare Ansi Function WritePrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As String,
      ByVal lpKeyName As String, ByVal lpString As String,
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function GetPrivateProfileInt _
      Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
      (ByVal lpApplicationName As String,
      ByVal lpKeyName As String, ByVal nDefault As Integer,
      ByVal lpFileName As String) As Integer
    Private Declare Ansi Function FlushPrivateProfileString _
      Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
      (ByVal lpApplicationName As Integer,
      ByVal lpKeyName As Integer, ByVal lpString As Integer,
      ByVal lpFileName As String) As Integer
    Dim strFilename As String

    ' Constructor, accepting a filename
    Public Sub New(ByVal Filename As String)
        strFilename = Filename
    End Sub

    ' Read-only filename property
    ReadOnly Property FileName() As String
        Get
            Return strFilename
        End Get
    End Property

    Public Function GetString(ByVal Section As String, ByVal Key As String, ByVal [Default] As String) As String
        ' Returns a string from your INI file
        Dim intCharCount As Integer
        Dim objResult As New System.Text.StringBuilder(256)
        intCharCount = GetPrivateProfileString(Section, Key, [Default], objResult, objResult.Capacity, strFilename)
        If intCharCount > 0 Then GetString = Left(objResult.ToString, intCharCount)
    End Function

    Public Function GetInteger(ByVal Section As String, ByVal Key As String, ByVal [Default] As Integer) As Integer
        ' Returns an integer from your INI file
        Return GetPrivateProfileInt(Section, Key, [Default], strFilename)
    End Function

    Public Function GetBoolean(ByVal Section As String, ByVal Key As String, ByVal [Default] As Boolean) As Boolean
        ' Returns a boolean from your INI file
        Return (GetPrivateProfileInt(Section, Key, CInt([Default]), strFilename) = 1)
    End Function

    Public Sub WriteString(ByVal Section As String, ByVal Key As String, ByVal Value As String)
        ' Writes a string to your INI file
        WritePrivateProfileString(Section, Key, Value, strFilename)
        Flush()
    End Sub

    Public Sub WriteInteger(ByVal Section As String, ByVal Key As String, ByVal Value As Integer)
        ' Writes an integer to your INI file
        WriteString(Section, Key, CStr(Value))
        Flush()
    End Sub

    Public Sub WriteBoolean(ByVal Section As String, ByVal Key As String, ByVal Value As Boolean)
        ' Writes a boolean to your INI file
        WriteString(Section, Key, CStr(CInt(Value)))
        Flush()
    End Sub

    Private Sub Flush()
        ' Stores all the cached changes to your INI file
        FlushPrivateProfileString(0, 0, 0, strFilename)
    End Sub
End Class
Posted
Updated 20-Oct-16 11:06am
Comments
[no name] 20-Oct-16 16:10pm    
"How to I acquire read-write codes for XML in VB.NET?", you use your keyboard and write it.
What does the code you have posted here, have anything at all to do with reading or writing XML?
MARUFB 20-Oct-16 16:40pm    
Probably yes.
[no name] 20-Oct-16 17:35pm    
What does "Probably yes." mean in this context? Are you unable to comprehend simple sentences?
MARUFB 21-Oct-16 6:16am    
Sorry. My English is low. So I can't write I mean.
ZurdoDev 20-Oct-16 16:26pm    
I do not follow what you are asking.

1 solution

You don't. The code you posted is for .INI files. The equivilent code for reading/writing XML files is already in the .NET Framework. The code you write will use those classes to read write against your XML schema for your particular application.

There's a variety of different ways to read/write/navigate/parse XML files. Which method you use depends on your application requirements.
 
Share this answer
 
v2
Comments
MARUFB 21-Oct-16 6:24am    
I use those codes, but not the way I want. Those code creates a new XML file. I want to add line to the XML file.
Dave Kreskowiak 21-Oct-16 10:03am    
XML is jsut a representation of your applications data model. Load the XML into your data model, update the model how to need to and then serialize the data model back into the XML file.

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