Advertisement

01.22.2008 at 02:15AM PST, ID: 23100643
[x]
Attachment Details

Create an XML Node with Attribute

Asked by advfinance in Visual Studio .NET 2005, Miscellaneous Programming, Extensible Markup Language (XML)

Tags: Visual Basic .NET, 2005

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("Bob", "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(strNode)
            objElement.InnerText = strValue
            objNode.InsertAfter(objElement, objNode.FirstChild)
            objXMLDoc.Save(strFilename)

            If strAttribute <> "" Then
                objXMLDoc.DocumentElement.SetAttribute(strAttribute, 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
[+][-]01.22.2008 at 03:32AM PST, ID: 20713316

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Visual Studio .NET 2005, Miscellaneous Programming, Extensible Markup Language (XML)
Tags: Visual Basic .NET, 2005
Sign Up Now!
Solution Provided By: shasunder
Participating Experts: 1
Solution Grade: B
 
 
[+][-]01.22.2008 at 05:20AM PST, ID: 20713885

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 07:20AM PDT, ID: 21368024

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628