我正在尝试将Xml
我准备好的一段文本添加到 vb.net 中的现有文档中,我正在使用XmlDocument
该类。这是我的代码(简化以解释问题):
Dim s As String = "<RelativeLayout android:layout_width=""fill_parent"" android:layout_height=""fill_parent"" android:layout_weight=""1.0"" />"
Dim tempdoc = New XmlDocument()
tempdoc.LoadXml("<doc />")
ns = New XmlNamespaceManager(tempdoc.NameTable)
ns.AddNamespace("android", "http://schemas.android.com/apk/res/android")
tempdoc.DocumentElement.SetAttribute("xmlns:android", "http://schemas.android.com/apk/res/android")
Dim frag = tempdoc.CreateDocumentFragment()
frag.InnerXml = s
最后一条指令生成一个 XmlException,上面写着“android prefix is undeclared”。我的印象是 XmlNamespaceManager(第 4-5 行)或直接编写命名空间属性(第 6 行)会解决这个问题,但显然不是。
我究竟做错了什么?
我知道我可以使用该createelement
方法手动编写元素,但我给出的示例被简化以解释问题。实际上,字符串"s"
是一个有很多属性和子节点的大块,Xml
用代码手动编写它会很痛苦。如果可能的话,我想要做的是将整个文件添加Xml
到文档中。