3

I've got an XML file that's something like this:

<globalConfigs>
  <!-- some settings -->
  <globalConfig key="currency" value="£" />
</globalConfigs>

And I've got some Powershell which is manipulating it along the lines of this:

function Update-Config ($filePath, $siteName) {
    [xml]$config = Get-Content $filePath;
    $newNode = $config.CreateElement("globalConfig");

    $keyAttribute = $config.CreateAttribute("key");
    $keyAttribute.Value = "currencyCode";
    $newNode.Attributes.Append($keyAttribute);

    $valAttribute = $config.CreateAttribute("value");
    $valAttribute.Value = "GBP";
    $newNode.Attributes.Append($valAttribute);

    $config.globalConfigs.AppendChild($newNode);
    Write-Log "Added config node to site.";
    $config.Save($filePath);
}

Now this works as expected in that it adds the new key. But the resulting xml adds an extra character in before the pound sign:

<globalConfigs>
  <!-- some settings -->
  <globalConfig key="currency" value="£" />
  <globalConfig key="currencyCode" value="GBP" />
</globalConfigs>

What is causing this extra character? It appears to be intermittent, but it's added more often than not.

2 Answers 2

1

Your XML contains a non-ASCII character without proper encoding. Either encode the character as an entity reference (&#163;), or add a declaration to your XML data:

function Update-Config ($filePath, $siteName) {
    [xml]$config = Get-Content $filePath;
    $newNode = $config.CreateElement("globalConfig");

    $keyAttribute = $config.CreateAttribute("key");
    $keyAttribute.Value = "currencyCode";
    $newNode.Attributes.Append($keyAttribute);

    $valAttribute = $config.CreateAttribute("value");
    $valAttribute.Value = "GBP";
    $newNode.Attributes.Append($valAttribute);

    $config.globalConfigs.AppendChild($newNode)
    Write-Log "Added config node to site."
    $declaration = $config.CreateXmlDeclaration('1.0', 'utf-8', $null)
    [void]$config.InsertBefore($declaration, $config.DocumentElement)
    $config.Save($filePath)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Adding a declaration doesn't fix it, and an entity reference won't work due to the way the XML is used.
1

I managed to fix this - the issue is in the Get-Content cmdlet. If I change the line that loads the XML to this:

[xml]$config = Get-Content $filePath -Encoding UTF8;

then I don't get the unwanted character in the output.

Comments

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.