【问题标题】:System.argumentException while inserting xml node into document将 xml 节点插入文档时出现 System.argumentException
【发布时间】:2014-06-09 10:40:39
【问题描述】:

我正在尝试将 xml 节点插入到 xml 文档中的另一个节点中,但是每当我测试它时,我都会不断地得到一个 "system.ArgumentException"。我尝试了几种插入节点的方法,但不知道如何修复它。

代码如下:

Dim content As String = "<name>" + songName + "</name><artist>" + songArtist + "</artist><album>" +   songAlbum + "</album>"
            Dim doc As New XmlDocument
            doc.Load(getPath())
            Dim i As XmlNode = doc.CreateElement("song")
            i.InnerXml = content

            Try
                list.AppendChild(i)
            Catch

            End Try

            doc.Save(getPath())
            Return True
        End If

    End If

我从单步执行和使用 try catch 语句得知错误是“要插入的节点来自不同的文档上下文”。它出现在代码中的“list.AppendChild(I)”行。

有人可以提供任何建议来解决此错误吗?

编辑

我尝试使用 doc.AppendChild() 而不是 list.AppendChild() 并收到错误消息(显示在 cmets 中)。

回顾以前的方法,我在文档中插入了另一个节点,我使用了“root.AppendChild()”,所以我很确定 list 是正确的使用变量

【问题讨论】:

  • 为什么是list.AppendChild(i) 而不是doc.AppendChild(i)
  • 哇,我从来没有注意到这一点!我会尝试改变它
  • 没有工作,使用 doc.AppendChild(I) 我收到一个错误“InvalidOperationException”,并显示一条消息“doc.appendChild(I) 不起作用,现在我收到一个“InvalidOperationException”一条消息:“此文档已经有一个 'DocumentElement' 节点”
  • 展示如何声明变量list。并尝试doc.DocumentElement.AppendChild(i),这将修复后面的异常...

标签: asp.net xml vb.net xpath


【解决方案1】:

据我了解,问题是您需要先导入文档,然后附加到它。请参阅下面的示例。

http://www.dailyfreecode.com/forum/node-inserted-different-document-40.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多