Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks in advance , what is the correct way to update more then one child nodes of a node , Here is my XML document

XML
<?xml version="1.0"?>
<Names>
  <Name>
    <First_Name>FN 1</First_Name>
    <Middle_Name>MN 1</Middle_Name>
    <Last_Name>LN 1</Last_Name>
    <Country_Code>001</Country_Code>
    <Area_Code>
    </Area_Code>
    <Phone_No>
    </Phone_No>
    <Email_Address>mail@email.com</Email_Address>
    <Address>Address 1</Address>
  </Name>
  <Name ID="2">
    <First_Name>FN2</First_Name>
    <Middle_Name>Mn2</Middle_Name>
    <Last_Name>Ln2</Last_Name>
    <Country_Code>02</Country_Code>
    <Area_Code>002</Area_Code>
    <Phone_No>0002</Phone_No>
    <Email_Address>email@mail2.com</Email_Address>
    <Address>
    </Address>
  </Name>
  </Names>


I am able to update 1st child node but on second child node it gives error ' "Object reference not set to an instance of an object. what i
want to achieve is to update all the child nodes, any kind of help will be welcomed


Function Edit() As Boolean
        Try
            'Dim xd As New XmlDocument()
            'xd.Load(App_Path)
            'Dim nod As XmlNodeList = xd.SelectNodes("//First_Name[. = '" & txtFName.Text & "']" & "/parent::node()/First_Name")

            'If nod IsNot Nothing Then
            '    For Each node In nod
            '        'nod.Item(0).ChildNodes(0).InnerText = txtMName.Text.Trim
            '        'nod.Item(0).ChildNodes(1).InnerText = txtLName.Text.Trim
            '    Next

            'End If

            Dim xd As New XmlDocument()
            xd.Load(App_Path)
            Dim nod As XmlNode = xd.SelectSingleNode("//First_Name[. = '" & txtFName.Text & "']" & "/parent::node()/First_Name")
            If nod IsNot Nothing Then
                nod.ChildNodes(0).InnerText = "33"
                nod.ChildNodes(1).InnerText = "333"
            End If

            Edit = True
        Catch ex As Exception

            MessageBox.Show(ex.Message)
            ctlStatus.Text = "Current State : " & ex.Message.ToString
            Return False
        End Try
        Return Edit
    End Function
Posted

1 solution

Both nodes you have displayed have different First_Name values. Your SelectSingleNode is looking for a specific node so of course you only have one node in the collection.

If you can, you should use Linq to XML. It makes traversing and manipulating XML very easy.
 
Share this answer
 
Comments
εїзεїзεїз 26-Dec-11 0:16am    
Thanks for Reply, Yes, That part is ok i want to Select One node and then want to update that Selected nodes child, i will try to do it with LINQ thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900