0. Apertura
Dim xdoc As New XmlDocument
Dim xnodelist As XmlNodeList
Dim xnode As XmlNode
Try
xdoc.LoadXml(stringaIn)
Catch ex As Exception
End Try
1. Lettura XML
‘selezione di una struttura innestata
xnodelist = xdoc.SelectNodes(“parametri/dati/dato”)
Dim tipo_dato As String
For Each xnode In xnodelist
‘Esempio di lettura di un valore in un sotto nodo
tipo_dato = xnode.SelectSingleNode(“tipo”).InnerText
next
1B. Lettura alternativa
Campo=xdoc.GetElementsByTagName(“NomeCampo”).Item(0).InnerText
2. Lettura con XmlTextReader
Dim sr As IO.StringReader = New IO.StringReader(StringaXML)
Dim tre As XmlTextReader
tre = New XmlTextReader(sr)
While tre.Read
If tre.NodeType = XmlNodeType.Element Then
Try
tre.MoveToAttribute(0)
DatoLettoUno = tre.Value
tre.MoveToAttribute(1)
DatoLettoDue = tre.Value
Catch ex As Exception
End Try
End If
End While
tre.Close()
3. Scrittura con XmlTextWriter
Dim sw As IO.StringWriter = New IO.StringWriter()
Dim ttW As XmlWriter = New XmlTextWriter(sw)
ttW.WriteStartDocument()
ttW.WriteStartElement(“Report”)
For Each field As PivotGridField In pivGrid.Fields
ttW.WriteStartElement(“campo”)
ttW.WriteAttributeString(“nome”, field.FieldName)
ttW.WriteAttributeString(“area”, field.AreaIndex)
ttW.WriteAttributeString(“ordine”, field.Area)
ttW.WriteEndElement()
Next field
ttW.WriteEndElement()
XMLStringOut = sw.ToString
3b. Sempre con XmlTextWriter (stile diverso)
Dim xd As New XmlDocument
Dim root, vv As XmlNode
root = xd.CreateNode(XmlNodeType.Element, “result”, “”)
xd.AppendChild(root)
vv = xd.CreateNode(XmlNodeType.Element, “status”, “”)
vv.InnerText = status
root.AppendChild(vv)
vv = xd.CreateNode(XmlNodeType.Element, “message”, “”)
vv.InnerText = messaggio
root.AppendChild(vv)
Dim sw As IO.StringWriter = New IO.StringWriter()
Dim xw As XmlTextWriter = New XmlTextWriter(sw)
xd.WriteTo(xw)
XMLStringOut = sw.ToString