问题描述
编辑:这与链接的帖子不太一样.我遇到的主要问题是将子节点附加到一个空的 XML 节点.当直接选择节点时,它会返回一个 System.String
类型,它没有 AppendChild
方法.解决方法是选择所有子节点,然后根据下面 Dan 的建议按名称过滤.
EDIT: This isn't quite the same as the posted that was linked. The main problem I was running into was appending child nodes to an empty XML node. When selecting the node directly, it would return a System.String
type, which doesn't have the AppendChild
method. The fix was to select all child nodes and then filter that by name, per Dan's suggestion below.
$emptyNode= $root.ChildNodes | ? { $_.name -eq "customers" }
我主要使用 powershell,但在我使用的许多代码下面使用的是 .NET System 对象.我想要做的可能是通过一个例子更好地解释.假设我有三个 XML 文档:
I'm mostly using powershell, but underneath a lot of the code I'm working with is using .NET System object. What I'm looking to do is probably better explained through an example. Say I have three XML documents:
<!-- XML File A -->
<customer>
<name>ACME Co</name>
<users>
<user>
<name>Alex</name>
<age>20</age>
</user>
<user>
<name>Aaron</name>
<age>21</age>
</user>
<user>
<name>Allison</name>
<age>22</age>
</user>
</users>
</customer>
和
<!-- XML File B -->
<customer>
<name>Big Co</name>
<users>
<user>
<name>Bob</name>
<age>30</age>
</user>
<user>
<name>Barry</name>
<age>31</age>
</user>
<user>
<name>Bill</name>
<age>32</age>
</user>
</users>
</customer>
和
<!-- XML File C -->
<customer>
<name>Cool Co</name>
<users>
<user>
<name>Carl</name>
<age>40</age>
</user>
<user>
<name>Craig</name>
<age>41</age>
</user>
<user>
<name>Chris</name>
<age>42</age>
</user>
</users>
</customer>
我有一个根"文件,看起来像:
I have a "root" document that looks like:
<?xml version='1.0' encoding='utf-8' ?>
<customers>
</customers>
我想合并根文档下的三个 A、B 和 C 文档,这样我的最终产品将是:
I want to combine the three A, B, and C documents under the root doc so my final product will be:
<?xml version='1.0' encoding='utf-8' ?>
<customers>
<!-- XML File A -->
<customer>
<name>ACME</name>
<users>
<user>
<name>Alex</name>
<age>20</age>
</user>
<user>
<name>Aaron</name>
<age>21</age>
</user>
<user>
<name>Allison</name>
<age>22</age>
</user>
</users>
</customer>
<!-- XML File B -->
<customer>
<name>Big Co</name>
<users>
<user>
<name>Bob</name>
<age>30</age>
</user>
<user>
<name>Barry</name>
<age>31</age>
</user>
<user>
<name>Bill</name>
<age>32</age>
</user>
</users>
</customer>
<!-- XML File C -->
<customer>
<name>Cool Co</name>
<users>
<user>
<name>Carl</name>
<age>40</age>
</user>
<user>
<name>Craig</name>
<age>41</age>
</user>
<user>
<name>Chris</name>
<age>42</age>
</user>
</users>
</customer>
</customers>
我一直在查看 AppendChild 和 ImportNode,但我不断收到各种错误.一件事是,在我的根文档中,单个空节点 customers
被列为 System.String 类型而不是 XmlNode,因此我无法附加任何子节点.请参阅此 Powershell 代码段:
I've been looking at AppendChild and ImportNode but I keep getting various errors. One thing, is that in my root document, the single empty node customers
is listed a type of System.String rather than an XmlNode, so I can't append any children. See this Powershell snippet:
$doc = New-Object System.Xml.XmlDocument
$doc.LoadXml("<?xml version='1.0' encoding='utf-8' ?><customers></customers>")
$doc.customers.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
不过,这甚至无关紧要,因为如果我尝试导入节点,则会收到错误无法导入文档"类型的节点.
.
That doesn't even matter that much though, because if I try to import the node, I get an error Cannot import nodes of type 'Document'.
.
$docA = New-Object System.Xml.XmlDocument
$docA.LoadXml("<customer><name>ACME</name><users><user><name>Alex</name><age>20</age></user><user><name>Aaron</name><age>21</age></user><user><name>Allison</name><age>22</age></user></users></customer>")
$docAImported = $doc.ImportNode($docA, $true)
Exception calling "ImportNode" with "2" argument(s): "Cannot import nodes of type 'Document'."
At line:1 char:32
+ $docAImported = $doc.ImportNode <<<< ($docA, $true)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
任何帮助将不胜感激.谢谢!
Any help would be greatly appreciated. Thanks!
推荐答案
通过执行以下操作首先获取客户节点:
Get the customer node first by doing the following:
$customersNode = $doc.ChildNodes | ? { $_.name -eq "customers" }
现在您可以使用文档 A、B 和 C 在 $customersNode 上调用 AppendChild.
Now you can call AppendChild on $customersNode with document A, B and C.
然而,导入文档 A、B 和 C,您几乎是正确的.使用 DocumentElement 属性,如下所示:
However to import document A, B and C you almost had it right. Use the DocumentElement property like below:
$docAImported = $doc.ImportNode($docA.DocumentElement, $true)
这篇关于将 XML 文档合并到空的 XML 节点中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!