Solved

Add attributes in xmlDocument

Posted on 2006-06-30
3
953 Views
Last Modified: 2013-11-19
Hi experts,

how can i write this tag in vb.net :

<node page="1/2">test</node>

for now, i have this :

Dim doc As New XmlDocument
Dim node As XmlNode

'HERE ! HOW CAN I ADD AN ATTRIBUTE TO THIS NODE?

node.innertext = "test"

doc.appenchild(node)

regards
0
Comment
Question by:Dnx_7
3 Comments
 
LVL 6

Accepted Solution

by:
manch earned 125 total points
ID: 17016289
U can use this function to Set a New Key Value to a XML File
'Here i have used Section1 and Section2 and different Sections (Node Levels of the XML File)
'u can change then as per ur requirement

(if the Node does not exists it will be created
    '---------------------------------------------------------------------------------------
    'SetKeyValue    
    '-----------------------------------------------------------------------------------
    Public Function SetKeyValue(ByVal XMLPath String, ByVal KeyName As String, _
                                        ByVal KeyValue As String) As Boolean
        '---------------------------------------------------------------------------------------
        'Function Name  :   SetKeyValue()
        'Purpose        :   Writes the KeyVale to specified Key (KeyName)
        '                           in the XML File Specified
        'Return Type    :   True (If Successfully Written) /False (On Error)
        '---------------------------------------------------------------------------------------

        Dim pObjXMLDocument As XmlDocument
        Dim pObjXMLNode As XmlNode
        Dim pObjXMLRoot As XmlNode
        Dim pObjXMLKey As XmlNode
        Dim pObjXMLValue As XmlNode
        Dim pbReturn As Boolean

        Try

            pbReturn = True

            pObjXMLDocument = New XmlDocument

            'Loading the XML File
            pObjXMLDocument.Load(XMLPath)

            pObjXMLNode = pObjXMLDocument.DocumentElement.SelectSingleNode("/Section1/Section2/add[@key=""" & KeyName & """]")

            'If the Key Not Found
            'Creating It
            If (pObjXMLNode Is Nothing) Then

                'The node does not exist, let's create it
                pObjXMLNode = pObjXMLDocument.CreateNode(XmlNodeType.Element, "add", "")

                'Adding the Key attribute to the node, keep in mind, Xml tokens are case sensitive
                'We should use 'key' instead of 'Key'

                pObjXMLKey = pObjXMLDocument.CreateNode(XmlNodeType.Attribute, "key", "")
                pObjXMLKey.Value = KeyName
                pObjXMLNode.Attributes.SetNamedItem(pObjXMLKey)

                'Adding the key value, once again, remember that Xml tokens are case sensitive

                pObjXMLValue = pObjXMLDocument.CreateNode(XmlNodeType.Attribute, "value", "")
                pObjXMLValue.Value = KeyValue
                pObjXMLNode.Attributes.SetNamedItem(pObjXMLValue)

                'Add the new node to the root
                pObjXMLRoot = pObjXMLDocument.DocumentElement.SelectSingleNode("/Section1/Section2")

                If (Not (pObjXMLRoot Is Nothing)) Then
                    pObjXMLRoot.AppendChild(pObjXMLNode)
                Else
                    pbReturn = False
                End If

            Else
                '
                'The node exist, save the new value
                pObjXMLNode.Attributes.GetNamedItem("value").Value = KeyValue

            End If

            pObjXMLDocument.Save(XMLPath)

            Return pbReturn

        Catch ex As Exception

            pbReturn = False
            Return pbReturn

        Finally

            If (Not (pObjXMLDocument Is Nothing)) Then pObjXMLDocument.dispose()
            If (Not (pObjXMLKey Is Nothing)) Then pObjXMLKey.dispose()
            If (Not (pObjXMLNode Is Nothing)) Then pObjXMLNode.dispose()
            If (Not (pObjXMLRoot Is Nothing)) Then pObjXMLRoot.dispose()
            If (Not (pObjXMLValue Is Nothing)) Then pObjXMLValue.dispose()

        End Try

    End Function
    '---------------------------------------------------------------------------------------
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now