1

I'm writing a script at the moment, the main aim of which is to locally interrogate a series of XML files on a target computer and re-create them if they are found to be corrupted or missing for whatever reason.

The problem that I'm having is that there is a particular XML file used by the FileZilla Server application that has no XML declaration and no indentation on it's nodes. As these values are static, I simply created a System.Xml.XmlDocument variable and copied the data within a working XML file and pasted it into the argument parentheses of the .LoadXml() function without indentation, like so;

$xmlFileZillaServer= New-Object System.Xml.XmlDocument
    $xmlFileZillaServer.LoadXml('
<FileZillaServer>
<Settings>
<Item name="Serverport" type="numeric">21</Item>
<Item name="Number of Threads" type="numeric">2</Item>
<Item name="Maximum user count" type="numeric">0</Item>
<Item name="Timeout" type="numeric">120</Item>
<Item name="No Transfer Timeout" type="numeric">120</Item>
                     etcetera...

However, the output of the XML is indented, as follows;

<FileZillaServer>
  <Settings>
    <Item name="Serverport" type="numeric">21</Item>
    <Item name="Number of Threads" type="numeric">2</Item>
    <Item name="Maximum user count" type="numeric">0</Item>
    <Item name="Timeout" type="numeric">120</Item>
    <Item name="No Transfer Timeout" type="numeric">120</Item>
                     etcetera...

How can I stop LoadXml from putting this indentation in automatically? Or am I going to have to use an XmlTextWriter object to write the XML manually and specify the indentation configuration during the write process?

Fredulom
  • 908
  • 1
  • 10
  • 23
  • 2
    It's not the `LoadXml()` method that adds the indentation, the document is automatically formatted when you `Save()` it. So yes, you *will* need to use a custom `XmlTextWriter`. – Mathias R. Jessen Nov 27 '15 at 13:07

2 Answers2

1

Or am I going to have to use an XmlTextWriter object to write the XML manually and specify the indentation configuration during the write process?

Yes, or rather, an XmlWriter class with a custom XmlWriterSettings object:

# Create a new settings object and configure settings
$XmlWriterSettings = New-Object System.Xml.XmlWriterSettings
$XmlWriterSettings.Indent = $false
$XmlWriterSettings.OmitXmlDeclaration = $true

# Create a new writer with the settings above
$XmlWriter = [System.Xml.XmlTextWriter]::Create("C:\output.xml",$XmlWriterSettings)

# Use the writer to save the document
$XmlFileZillaServer.Save($XmlWriter)
Fredulom
  • 908
  • 1
  • 10
  • 23
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thanks @Mathias, I figured that was the case but I appreciate your assistance :) – Fredulom Nov 30 '15 at 10:25
  • Actually, having had a look at this, setting the `Indent` option to `$false` makes the `XMLTextWriter` object output the values on one line. – Fredulom Dec 02 '15 at 10:41
0

I've found the answer to this issue through a combination of trial-and-error and having a browse through other StackOverflow threads. Essentially, when I set the Indent property to $false made the XML document write on one long line.

Having had a look at this thread for the XmlTextWriter class in a C# thread, you instead need to set Indent to $true and then set the IndentChars value to be nothing ('') - see below for my method:

$xmlWriterSettings= New-Object System.Xml.XmlWriterSettings
$xmlWriterSettings.Indent= $true
$xmlWriterSettings.IndentChars= ''
$xmlWriterSettings.OmitXmlDeclaration= $true

### INITIALISE XML WRITER ###
$xmlWriter= [System.Xml.XmlTextWriter]::Create("C:\FileZilla Server.xml",$xmlWriterSettings)
Community
  • 1
  • 1
Fredulom
  • 908
  • 1
  • 10
  • 23