1

I am dealing with a bunch of VOD meta data files (XML). There is an ADI.DTD that defines the schema of my xml.

I am trying to use powershell to edit this xml and update/change a tag using the following piece of powershell code...

[System.Xml.XmlDocument]$doc = 
       new-object System.Xml.XmlDocument;
    $doc.Load($strXMLFile);
    $root = $doc.get_DocumentElement();
    $root.Fruit.Metadata.AMS.Name = $strName
    $doc.Save($strXMLFile)

Though it is pretty much the same I also tried doing..

$xmlDoc = [XML](gc $strXMLFile) 
$xmldoc.Fruit.Metadata.AMS.Name = $strName
$xmldoc.Save($strXMLFile)

This save is changing more than the one tag I wanted it to. It is deleting few carriage returns and appending this weird [] to my DOCTYPE.. I don't understand why it is doing it and don't know the downward impact on systems that consume this XML.

<!DOCTYPE ADI SYSTEM "ADI.DTD">

to

<!DOCTYPE ADI SYSTEM "ADI.DTD"[]>

Any inputs is appreciated

Kir
  • 385
  • 1
  • 7
  • 20

1 Answers1

1

To keep the carriage returns try setting this property before saving:

$xmlDoc.PreserveWhitespace = $true

The square brackets are valid XML and shouldn't have any negative effect. See this SO question:

XmlDocument.Save() inserts empty square brackets in doctype declaration

Community
  • 1
  • 1
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Thank you for the quick response. I don't know why my search did not lead me to this obviously answered question. Thank you. – Kir May 18 '12 at 19:01