Solved

Add attributes in xmlDocument

Posted on 2006-06-30
3
970 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

623 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