English 中文(简体)
使用 powershell 附加到/ 移动空 xml 节点
原标题:Appending to/Removing empty xml nodes using powershell

PowerShell/xml beginner here.... I m trying to append to or remove empty xml nodes using PowerShell as part of a Nuget Package. The xml file has the following format...

<Root>
    <service name="first">
        <item>
        </item>
    </service>
    <service name ="second">
        <item>
        </item>
    </service>
</Root>

首先,我的脚本选择一个服务, 并保存为变量, 如果用户想要选择服务 1...

if ($xml.Root.service.name -eq $serviceName)
{
         $myService = $xml.Root.service
}

Problem is later on, I need to append elements to the node/delete the node... I have something like

    $newNode = $xml.CreateElement( new ...
    .........

    $empty = $myService.SelectSingleNode( ./item )
    $empty.PrependChild($newNode)

但我不能让这个方法工作。

如有任何建议,将不胜感激。

最佳回答

这应该能帮到你

# Get an XML document
$MyXml = [xml] <?xml version="1.0" encoding="utf-8"?><root><service name="foo"><item></item></service></root> ;
# Create a new element from the XmlDocument object
$NewElement = $MyXml.CreateElement( new );
# Select the element that we re going to append to
$ServiceElement = Select-Xml -Xml $MyXml -XPath  /root/service[@name="foo"]/item ;
# Append the  new  element to the  item  element
$ServiceElement.AppendChild($NewElement);
# Echo the OuterXml property of the $MyXml variable to verify changes
Write-Host -Object $MyXml.OuterXml;
# Save the XML document
$MyXml.Save( c:	est.xml );
问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签