使用 powershell 生成的空 xmlns 属性进行处理
Handle with empty xmlns attribute generated by powershell
我正在尝试使用 powershell 创建 xml 文件,它在我的第二层节点中添加了一个空的 xmlns=""
。 Google说是父子的namespace不一样,生成的时候怎么弄一样?
我的 powershell 代码如下
$gNamespace = "http://schemas.microsoft.com/wix/2006/wi"
$xmlFilePath = "PATH_TO_XML_FILE"
$xmlWriter = New-Object System.XMl.XmlTextWriter($xmlFilePath, $Null)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement("Wix", $gNamespace)
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Finalize
$xmlWriter.Flush()
$xmlWriter.Close()
$xmlDoc = [System.Xml.XmlDocument](Get-Content $xmlFilePath);
$nsmgr = new-object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
$nsmgr.AddNamespace("wixns", $gNamespace)
$xmlDoc = [System.Xml.XmlDocument](Get-Content $xmlFilePath)
$productNode = $xmlDoc.CreateElement("Product")
$xmlDoc.SelectSingleNode("//wixns:Wix", $nsmgr).AppendChild($productNode)
$productNode.SetAttribute("Id", "ProductIdHere")
上面的代码生成以下内容xml
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="ProductIdHere" xmlns=""/>
</Wix>
我的预期结果是
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="ProductIdHere"/>
</Wix>
powershell生成xml时如何使父子命名空间相同?以及 select 具有命名空间的正确节点?
感谢您的帮助。
在 PowerShell 中有更简单的方法可以做到这一点。看看这个例子 here
从这里您可以创建您的 XML 例如如下:
[xml]$doc = New-Object System.Xml.XmlDocument
$dec = $doc.CreateXmlDeclaration("1.0", $null, $null)
$doc.AppendChild($dec)
$root = $doc.CreateNode("element","WiX", "http://schemas.microsoft.com/wix/2006/wi")
$c = $doc.CreateNode("element", "Product", "http://schemas.microsoft.com/wix/2006/wi")
$c.SetAttribute("Id", "ProductIdHere") | Out-Null
$root.AppendChild($c) | Out-Null
$doc.AppendChild($root) | Out-Null
$doc.Save($pathToSave)
您当然也可以只将基本结构存储在字符串或文件中,然后将其转换为 [xml]
,然后再执行一些操作。
我正在尝试使用 powershell 创建 xml 文件,它在我的第二层节点中添加了一个空的 xmlns=""
。 Google说是父子的namespace不一样,生成的时候怎么弄一样?
我的 powershell 代码如下
$gNamespace = "http://schemas.microsoft.com/wix/2006/wi"
$xmlFilePath = "PATH_TO_XML_FILE"
$xmlWriter = New-Object System.XMl.XmlTextWriter($xmlFilePath, $Null)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement("Wix", $gNamespace)
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Finalize
$xmlWriter.Flush()
$xmlWriter.Close()
$xmlDoc = [System.Xml.XmlDocument](Get-Content $xmlFilePath);
$nsmgr = new-object System.Xml.XmlNamespaceManager($xmlDoc.NameTable)
$nsmgr.AddNamespace("wixns", $gNamespace)
$xmlDoc = [System.Xml.XmlDocument](Get-Content $xmlFilePath)
$productNode = $xmlDoc.CreateElement("Product")
$xmlDoc.SelectSingleNode("//wixns:Wix", $nsmgr).AppendChild($productNode)
$productNode.SetAttribute("Id", "ProductIdHere")
上面的代码生成以下内容xml
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="ProductIdHere" xmlns=""/>
</Wix>
我的预期结果是
<?xml version="1.0"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="ProductIdHere"/>
</Wix>
powershell生成xml时如何使父子命名空间相同?以及 select 具有命名空间的正确节点?
感谢您的帮助。
在 PowerShell 中有更简单的方法可以做到这一点。看看这个例子 here
从这里您可以创建您的 XML 例如如下:
[xml]$doc = New-Object System.Xml.XmlDocument
$dec = $doc.CreateXmlDeclaration("1.0", $null, $null)
$doc.AppendChild($dec)
$root = $doc.CreateNode("element","WiX", "http://schemas.microsoft.com/wix/2006/wi")
$c = $doc.CreateNode("element", "Product", "http://schemas.microsoft.com/wix/2006/wi")
$c.SetAttribute("Id", "ProductIdHere") | Out-Null
$root.AppendChild($c) | Out-Null
$doc.AppendChild($root) | Out-Null
$doc.Save($pathToSave)
您当然也可以只将基本结构存储在字符串或文件中,然后将其转换为 [xml]
,然后再执行一些操作。