我如何将<Description>
标记及其值添加到以下xmla文件(xmla = Microsoft Analysis Services)中作为<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>
答案 0 :(得分:1)
$xml.FirstChild.AppendChild($xmlElt);
您的代码将新创建的元素附加到XML文档的第一个子节点。在您的情况下,似乎是文档根元素。它只能起作用,因为您的XML文件显然不包含声明(<?xml ...?>
),否则您将收到错误。
您需要实际选择要添加新元素的节点,然后在该节点上调用AppendChild()
。
$parent = $xml.SelectSingleNode('//d:Batch/d:Alter/d:Object', $ns)
$parent.AppendChild($xmlElt)