如何在vb.net中更新多个子节点 [英] how to Update more then one child nodes in vb.net

查看:63
本文介绍了如何在vb.net中更新多个子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

预先感谢,什么是更新一个节点的一个以上子节点的正确方法,这是我的XML文档

Thanks in advance , what is the correct way to update more then one child nodes of a node , Here is my XML document

<?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
想要实现的是更新所有子节点,欢迎任何形式的帮助




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

推荐答案

您显示的两个节点都有不同的First_Name值.您的SelectSingleNode正在寻找一个特定的节点,因此您在集合中当然只有一个节点.

如果可以,则应使用Linq to XML.它使遍历和操作XML非常容易.
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.


这篇关于如何在vb.net中更新多个子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆