在 XML 中的特定位置添加节点
Adding a node at a specific location in XML
我将如何在以下 xmla 文件 (xmla = Microsoft Analysis Services) 中添加一个 <Description>
标记及其值作为 <Object>
中的子元素?
这只是将其添加到文件末尾,而不是在 <Object>
.
下
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectSingleNode("//d:Database/d:Description", $ns)
$xmlElt = $xml.CreateElement("Description")
$xmlText = $xml.CreateTextNode("Mach1")
$xmlElt.AppendChild($xmlText)
$xml.FirstChild.AppendChild($xmlElt);
$xml.SelectSingleNode("//d:Database/d:Description", $ns)
$xml.Save("test.xml")
这是 xmla(顶部):
<Batch Transaction="false" xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Alter AllowCreate="true" ObjectExpansion="ExpandFull">
<Object>
<DatabaseID>CRH_TA1</DatabaseID>
</Object>
<ObjectDefinition>
<Database xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400">
<ID>CRH_TA1</ID>
<Name>CRH_TA1</Name>
<DataSourceImpersonationInfo>
<ImpersonationMode>ImpersonateAccount</ImpersonationMode>
$xml.FirstChild.AppendChild($xmlElt);
您的代码将新创建的元素附加到 XML 文档的第一个子节点。在您的情况下,这似乎是文档根元素。它之所以有效,是因为您的 XML 文件显然不包含声明 (<?xml ...?>
),否则您会收到错误消息。
您实际上需要 select 您要将新元素附加到的节点,然后在该节点上调用 AppendChild()
。
$parent = $xml.SelectSingleNode('//d:Batch/d:Alter/d:Object', $ns)
$parent.AppendChild($xmlElt)
我将如何在以下 xmla 文件 (xmla = Microsoft Analysis Services) 中添加一个 <Description>
标记及其值作为 <Object>
中的子元素?
这只是将其添加到文件末尾,而不是在 <Object>
.
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectSingleNode("//d:Database/d:Description", $ns)
$xmlElt = $xml.CreateElement("Description")
$xmlText = $xml.CreateTextNode("Mach1")
$xmlElt.AppendChild($xmlText)
$xml.FirstChild.AppendChild($xmlElt);
$xml.SelectSingleNode("//d:Database/d:Description", $ns)
$xml.Save("test.xml")
这是 xmla(顶部):
<Batch Transaction="false" xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
<Alter AllowCreate="true" ObjectExpansion="ExpandFull">
<Object>
<DatabaseID>CRH_TA1</DatabaseID>
</Object>
<ObjectDefinition>
<Database xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400">
<ID>CRH_TA1</ID>
<Name>CRH_TA1</Name>
<DataSourceImpersonationInfo>
<ImpersonationMode>ImpersonateAccount</ImpersonationMode>
$xml.FirstChild.AppendChild($xmlElt);
您的代码将新创建的元素附加到 XML 文档的第一个子节点。在您的情况下,这似乎是文档根元素。它之所以有效,是因为您的 XML 文件显然不包含声明 (<?xml ...?>
),否则您会收到错误消息。
您实际上需要 select 您要将新元素附加到的节点,然后在该节点上调用 AppendChild()
。
$parent = $xml.SelectSingleNode('//d:Batch/d:Alter/d:Object', $ns)
$parent.AppendChild($xmlElt)