Hi, I am creating a wrapper class in my application and have come across some difficulties and was
wondering if somebody could help me before I go insane. I am an advanced VB6 developed and new to VB.NET so however complex the solution is, don't worry I'm sure I'll get my head round it.
I have created a function that will add a node and value to the XML and I am attempting to add an attribute Node and value at the same time. My code is as follows, although ignore the variable objXMLDoc as I already have this created elsewhere.
objWrapperClass.AddNode("B
ob", "Bob is Cool!", "BobsFriend", "Bobs Friend is Cool!")
Public Function AddNode(ByVal strNode As String, ByVal strValue As String, _
Optional ByVal strAttribute As String = "", Optional ByVal strAttributeValue As String = "") As Boolean
Try
Dim objNode As XmlNode = objXMLDoc.DocumentElement
Dim objElement As XmlElement = objXMLDoc.CreateElement(st
rNode)
objElement.InnerText = strValue
objNode.InsertAfter(objEle
ment, objNode.FirstChild)
objXMLDoc.Save(strFilename
)
If strAttribute <> "" Then
objXMLDoc.DocumentElement.
SetAttribu
te(strAttr
ibute, strAttributeValue)
End If
objXMLDoc.Save(strFilename
)
InsertNodeValue = True
Catch ex As Exception
AddNode = False
End Try
End Function
Before I call the function the xml file contains following XML string:
<Container></Container>
After I call the function I get this:
<Container BobsFriend="Bobs Friend is Cool!"><Bob>Bob is Cool!</Bob></Container>
The problem is that the attribute 'BobFriend' is appearing in the 'Container' tag and not in the 'Bob' tag. I have a feeling that after I insert the node 'Bob' I have to set this as the current node first or something like that.
All the help I can get on this would be much appreciated. Many thanks. :oZ
Start Free Trial