如何使用XMLWriter()在PowerShell中正确创建此XML文件?
我想在PowerShell中使用XMLWRITER()创建这样的XML文档,但是我遇到了麻烦,这是预期的输出:
<Item version="1">
<Name>Myproject</Name>
<GUID>4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9</GUID>
</Item>
当前代码:
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$XmlWriter = [System.XML.XmlWriter]::Create("$PSScriptRoot\Myproject.xml", $xmlsettings)
# As mentioned above, I don't want the default output of WriteStartDocument(), so I used this
$xmlWrite.WriteStartElement("Item")
$xmlWrite.WriteAttributeString("Version", "1.0")
$xmlWriter.WriteElementString("Name",$myProject) # I have a string variable containing the project name
<#From here it throws the error: Exception calling "WriteElementString" with "2" argument(s): "Token StartElement in state EndRootElement would result in an
| invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if
| you want to write an XML fragment."#>
$xmlWriter.WriteElementString("GUID",(New-Guid).Guid.ToUpper())
$xmlWriter.WriteEndElement()
$xmlWriter.Flush()
$xmlWriter.Close()
上述代码的错误输出:
<?xml version="1.0" encoding="utf-8"?>
<Name>Myproject</Name>
不知道为什么仍然输出WritestArtDocument()的内容内容() ,我确定我没有键入错误的代码,这显然没有应用缩进。
有人愿意提供帮助吗?提前致谢!
I want to create an xml document like this using XmlWriter() in powershell but I'm having trouble, this is the expected output:
<Item version="1">
<Name>Myproject</Name>
<GUID>4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9</GUID>
</Item>
Current code:
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$XmlWriter = [System.XML.XmlWriter]::Create("$PSScriptRoot\Myproject.xml", $xmlsettings)
# As mentioned above, I don't want the default output of WriteStartDocument(), so I used this
$xmlWrite.WriteStartElement("Item")
$xmlWrite.WriteAttributeString("Version", "1.0")
$xmlWriter.WriteElementString("Name",$myProject) # I have a string variable containing the project name
<#From here it throws the error: Exception calling "WriteElementString" with "2" argument(s): "Token StartElement in state EndRootElement would result in an
| invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if
| you want to write an XML fragment."#>
$xmlWriter.WriteElementString("GUID",(New-Guid).Guid.ToUpper())
$xmlWriter.WriteEndElement()
$xmlWriter.Flush()
$xmlWriter.Close()
Error output of the above code:
<?xml version="1.0" encoding="utf-8"?>
<Name>Myproject</Name>
Do not know why still output the contents of WriteStartDocument(), I'm sure I didn't type the wrong code and this obviously doesn't apply the indentation.
Anyone willing to help? thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
xdocument
,这种事情更容易 far 。实际上,您只应该使用XMLWRITER
作为非常自定义的XML。输出
This kind of thing is far easier using
XDocument
. You are really only supposed to useXmlWriter
for very customized XML.Output
尝试一下 - &gt; $ XMLWRITER.WRITEELEMENTSTRING(“名称”,“ $($ myProject)”)
Try this -> $xmlWriter.WriteElementString("Name","$($myProject)")
您当前的代码不会编译,但是使用更正后的变量名称,它可以正常运行。
如果您需要删除XML版本,只需添加
$ XMLSETTINGS.OMITXMLDECLARATION = $ true
结果
Your current code does not compile, but with corrected variable names it works fine.
if you need remove xml version just add
$xmlsettings.OmitXmlDeclaration = $true
Result