appending text to an xml file using powershell
Hi,I had the below powershell code to append the text to an xml file..Can you please suggest or gimme the suitable code in powershell.In this I am trying to append the text to an xml file. with the following tags:
StreamWriter sw = null;
FileStream sr = File.Open("E:\\Newxml", FileMode.Append);
sw = new System.IO.StreamWriter(sr);
sw.Write("</step>");
sw.Write("</build>");
sw.Close();
sr.Close();
sr = null;
sw = null;Thanks in advance!!
December 19th, 2008 5:25pm
The easiest way to do a simple task as adding "</step>" and "</build>" would be to do thisAdd-Content e:\newxml.xml "</step></build>" or if you want the second piece of text on the next line you can doAdd-Content e:\newxml.xml "</step>`n</build>"The backtick ` with the n option does a new line.To do more advance functions with XML files other than appending text I would suggest getting familiar with import-xml cmdlet to import the xml file to a variable then using the available methods and properties to add more complex content.
Sr. Exchange Engineer - Constellation Energy
Free Windows Admin Tool Kit Click here and download it now
December 19th, 2008 5:59pm
Thanks Aron!But, let me explain you my complete task:I am reading the buildlog file(which is getting updated by the build tool duringbuild execution).My target is to dump the xml file into the database.When I say addadd-content $templatefile "</step></build</log>" the tags are appeding to the xml file but it is getting appended to the middle of the file.... it should append at the end of the file to make well form document.Because I need to supply well form document to ReadOuterxml method of xmltextreader....it is throwing me the below error:
Exception calling "ReadInnerXml" with "0" argument(s): "'<' is an unexpected to
ken. The expected token is '>'. Line 201, position 41."I tried with get-content to load the xml file,, but it is not performing good for large xml files like 1-2MB. so I had chosen the option to go for xmltextreader to process this file.Please let me know for the solution to read the xml file which is under process of build tool and to dump the complete xml content into the sql database.Your help is highly appreciated.!!!
$Srv="servername"$db="dbname"$templatefile = $args[7]#add-content $templatefile "</step></build</log>"[System.IO.FileStream] $fileStream = new-object System.IO.FileStream $templatefile, ([System.IO.FileMode]::Open),([System.IO.FileAccess]::Read)#add-content $filestream "</step></build</log>"[System.Xml.XmlTextReader]$rxmldr = new-object System.Xml.XmlTextReader $fileStream
while($rxmldr.Read()) {$xmlcontent=$rxmldr.ReadOuterXML();}
$conn = New-Object System.Data.SqlClient.SqlConnection("Server=$Srv;Database=$db;Integrated Security=True")$conn.Open() $cmd = new-Object System.Data.SqlClient.SqlCommand("BuildStatusSingleUpdate", $conn)$cmd.CommandType = [System.Data.CommandType] 'StoredProcedure'$cmd.Parameters.Add("@XMLData","$xmlcontent") $rdr = $cmd.ExecuteReader()$conn.Close()
December 19th, 2008 7:29pm
Dear customer:
Since you issue is related to PowerShell script, our forum focuses on Exchange server-related issue, it is inappropriate to solve your problems. I suggest that you will be send the issue intoWindows PowerShell forum.
Windows PowerShell
http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/threads/
Thanks for your understanding and cooperation.Rock Wang MSFT
Free Windows Admin Tool Kit Click here and download it now
December 26th, 2008 9:17am