Solved

Add attributes in xmlDocument

Posted on 2006-06-30
3
950 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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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

11 Experts available now in Live!

Get 1:1 Help Now