Creating classes, and using XML serialization, can make it easier to work with XML-especially if you are both reading and updating the XML.
To start, first look at the structure of the XML.
- Products
-Categoria
-riga1
-riga2
-riga3
We'll create a class for "Products" and also for "Categoria".
- Products: contains "Categoria"
- ProductsCategoria: contains attributes for "Categoria", as well as "riga1", "riga2", "riga3"
Create a class (name: ProductsCategoria.vb)
ProductsCategoria.vb
Imports System.Xml.Serialization
<XmlType(TypeName:="Categoria")>
Public Class ProductsCategoria
<XmlAttribute(AttributeName:="nome")>
Public Property Nome As String
<XmlElement(ElementName:="riga1")>
Public Property riga1 As String
<XmlElement(ElementName:="riga2")>
Public Property riga2 As String
<XmlElement(ElementName:="riga3")>
Public Property riga3 As String
Sub New()
End Sub
Sub New(nome As String, riga1 As String, riga2 As String, riga3 As String)
'set values
Me.Nome = nome
Me.riga1 = riga1
Me.riga2 = riga2
Me.riga3 = riga3
End Sub
End Class
There wasn't enough data supplied in the OP to know if "Products" may contain more than one "Categoria", but from the name "Products" being the plural form of "Product", it gives indication that it may contain more than one "Categoria". Therefore, we'll use a List of "Categoria" (ProductsCategoria). "riga1", "riga2", and "riga3" are all unique names, so they won't use a List.
Create a class (name: Products.vb)
Products:
Imports System.Xml.Serialization
<XmlRoot(ElementName:="Products")>
Public Class Products
<XmlElement(ElementName:="Categoria")>
Public Categorias As New List(Of ProductsCategoria)
End Class
Next, we'll write our code to both read the XML from file (deserialize) and write the XML to file (serialize).
Create a module (name: HelperXml.vb)
HelperXml.vb
Module HelperXml
Public Function DeserializeXMLFileToObject(Of T)(xmlFilename As String) As T
'usage
'Dim myClass1 As Class1 = DeserializeXMLFileToObject(Class1)(xmlFilename)
Dim rObject As T = CType(Nothing, T)
Try
If String.IsNullOrEmpty(xmlFilename) Then
Return rObject
End If
Using xmlStream As System.IO.StreamReader = New System.IO.StreamReader(xmlFilename)
'create new instance
Dim serializer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(GetType(T))
'read XML from file
rObject = CType(serializer.Deserialize(xmlStream), T)
End Using
Catch ex As Exception
Debug.WriteLine("Error (DeserializeXMLFileToObject) - " & ex.Message)
Throw ex
End Try
Return rObject
End Function
Public Sub SerializeObjectToXMLFile(obj As Object, xmlFilename As String)
'Usage:
'Dim myClass1 As Class1 = New Class1()
'SerializeObjectToXMLFile(myClass1, xmlFilename)
Try
If String.IsNullOrEmpty(xmlFilename) Then
Return
End If
Dim settings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
settings.Encoding = System.Text.Encoding.UTF8
settings.OmitXmlDeclaration = False
settings.Indent = True
Using xmlWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(xmlFilename, settings)
'specify namespaces
Dim ns As System.Xml.Serialization.XmlSerializerNamespaces = New System.Xml.Serialization.XmlSerializerNamespaces()
ns.Add(String.Empty, "urn:none")
xmlWriter.WriteProcessingInstruction("xml", "version=""1.0"" encoding=""UTF-8"" standalone=""yes""")
'create new instance
Dim serializer As System.Xml.Serialization.XmlSerializer = New System.Xml.Serialization.XmlSerializer(obj.GetType())
'write XML to file
serializer.Serialize(xmlWriter, obj, ns)
End Using
Catch ex As Exception
Debug.WriteLine("Error (SerializeObjectToXMLFile) - " & ex.Message)
Throw ex
End Try
End Sub
End Module
Usage:
Private prods As New Products
Private xmlFilename As String = New String("C:\Temp\test_products.xml")
...
Private Sub ChangeName(index As Integer, name As String)
If prods IsNot Nothing AndAlso prods.Categorias.Count > 0 AndAlso index >= 0 AndAlso index < prods.Categorias.Count Then
prods.Categorias(index).Nome = name
'Write to file
SerializeObjectToXMLFile(prods, xmlFilename)
End If
End Sub
Private Sub ReadXmlFromFile()
prods = HelperXml.DeserializeXMLFileToObject(Of Products)(xmlFilename)
End Sub
Private Sub WriteXmlToFile()
prods.Categorias.Add(New ProductsCategoria("etichetta", "val1", "val2", "val3"))
SerializeObjectToXMLFile(prods, xmlFilename)
End Sub
Resources:
nuevo_nomeand then the resulting XML you want? As you selected theCategoriaelement withSelectSingleNodeI thought you want to change that element's content, but perhaps you want to select or change only thenomeattribute value.