Solved

Add attributes in xmlDocument

Posted on 2006-06-30
3
955 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

785 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