VB.Net Class property to get and/or set the value of an XML node
By Allen Stoner
Here is the property code to get the XML element value from a XML document. And the counterpart to that is the set method that either sets the value of the element if it exists or creates it and sets the value if necessary.
Public Property DataValue(ByVal DataField As String, Optional ByVal DefaultValue As String = "") As String
Get
Try
DataValue = xData.SelectSingleNode("/FormData/Data[@Name='" & DataField & "']").InnerText
Catch ex As Exception
DataValue = DefaultValue
End Try
End Get
Set(ByVal value As String)
Dim xNode As Xml.XmlNode
xNode = xData.SelectSingleNode("/FormData/Data[@Name='" & DataField & "']")
If xNode Is Nothing Then
xNode = xData.CreateNode(XmlNodeType.Element, "", "Data", "")
Dim xAttr As Xml.XmlAttribute
xAttr = xData.CreateAttribute("Name")
xAttr.InnerText = DataField
xNode.Attributes.Append(xAttr)
xData.DocumentElement.AppendChild(xNode)
End If
Try
xNode.InnerText = value
Catch ex As Exception
' Handle errors as desired here
End Try
End Set
End Property
VB.Net Class property to get and/or set the value of an XML node (1556 Views)