如何使用XMLWriter()在PowerShell中正确创建此XML文件?

发布于 2025-02-13 11:32:19 字数 1417 浏览 0 评论 0原文

我想在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖伴 2025-02-20 11:32:20

使用xdocument,这种事情更容易 far 。实际上,您只应该使用XMLWRITER作为非常自定义的XML。

using namespace System.Xml.Linq;

$doc = [XDocument]::new(
   [XElement]::new("Item",
     [XAttribute]::new("version", "1"),
     [XElement]::new("Name", "Myproject"),
     [XElement]::new("GUID", "4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9")
   )
 );

$doc.ToString();
$doc.ToString() | Out-File "$PSScriptRoot\Myproject.xml";

输出

<Item version="1">
  <Name>Myproject</Name>
  <GUID>4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9</GUID>
</Item>

This kind of thing is far easier using XDocument. You are really only supposed to use XmlWriter for very customized XML.

using namespace System.Xml.Linq;

$doc = [XDocument]::new(
   [XElement]::new("Item",
     [XAttribute]::new("version", "1"),
     [XElement]::new("Name", "Myproject"),
     [XElement]::new("GUID", "4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9")
   )
 );

$doc.ToString();
$doc.ToString() | Out-File "$PSScriptRoot\Myproject.xml";

Output

<Item version="1">
  <Name>Myproject</Name>
  <GUID>4821CC01-CDB0-4FD7-9F1E-0B1EDF32ACE9</GUID>
</Item>
栀梦 2025-02-20 11:32:20

尝试一下 - &gt; $ XMLWRITER.WRITEELEMENTSTRING(“名称”,“ $($ myProject)”)

Try this -> $xmlWriter.WriteElementString("Name","$($myProject)")

亣腦蒛氧 2025-02-20 11:32:19

您当前的代码不会编译,但是使用更正后的变量名称,它可以正常运行。

如果您需要删除XML版本,只需添加$ XMLSETTINGS.OMITXMLDECLARATION = $ true

$myProject = 'Myproject'
$path = "c:\temp\Myproject.xml"

$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true

$xmlWriter = [System.XML.XmlWriter]::Create($path, $xmlsettings)
$xmlWriter.WriteStartElement("Item") 
$xmlWriter.WriteAttributeString("Version", "1.0")
$xmlWriter.WriteElementString("Name",$myProject)
$xmlWriter.WriteElementString("GUID",(New-Guid).Guid.ToUpper())
$xmlWriter.WriteEndElement()

$xmlWriter.Flush()
$xmlWriter.Close()

结果

<?xml version="1.0" encoding="utf-8"?>
<Item Version="1.0">
  <Name>Myproject</Name>
  <GUID>3CA7FBFA-1585-44A1-B4D9-D59E9371FE1B</GUID>
</Item>

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

$myProject = 'Myproject'
$path = "c:\temp\Myproject.xml"

$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true

$xmlWriter = [System.XML.XmlWriter]::Create($path, $xmlsettings)
$xmlWriter.WriteStartElement("Item") 
$xmlWriter.WriteAttributeString("Version", "1.0")
$xmlWriter.WriteElementString("Name",$myProject)
$xmlWriter.WriteElementString("GUID",(New-Guid).Guid.ToUpper())
$xmlWriter.WriteEndElement()

$xmlWriter.Flush()
$xmlWriter.Close()

Result

<?xml version="1.0" encoding="utf-8"?>
<Item Version="1.0">
  <Name>Myproject</Name>
  <GUID>3CA7FBFA-1585-44A1-B4D9-D59E9371FE1B</GUID>
</Item>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文