2

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!

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Well, that is, parameter outputFileName only accepts absolute paths, solved, thanks for your answer!
3

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>

1 Comment

This is also a helpful answer, thanks for your answer!
0

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

1 Comment

No, the output of the variable $myProject is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.