Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, August 22, 2023

PowerShell to write XML with attributes on new line

Context

Many times after patch deployment of the application into the server there will be questions on what changed? The source code is obviously changed and the binaries can't be compared to find out what changed.

But the textual configuration files can easily be compared. Take a backup of existing files. Do the patching and compare what changed.

This post is assuming the deployment tools are using PowerShell to change the config files.

Problem

Look at the image below.

It shows there are a lot of changes done to the file. But really there is only one change. dbserver3->dbserver2

As we can easily understand the comparison considered the files as text, not as XML, and found a lot of differences.

Approaches

If the number of files is less this is fine but that is not the case in enterprise. There are multiple approaches to this problem.

A tool that understands XML and compares

The above comparison tool is not recognizing the XML format instead it compared textually. Find another tool that understands the XML format and compare accordingly.

To date, we didn't find any such tool. Please comment if there are tools that are free and export the comparison report.

Always have attributes on a new line

In this approach make sure all the tools and programs that modify the XML write attributes on a new line.
Since PowerShell is running on top of .Net it is easy to get anything working in PowerShell that works in .Net. Google easily lands us in pages that say use the XMLWriterSettings.NewLineOnAttributes to get the job done. Unfortunately, it is not working out of the box.

But when we combine the .Indent property with .NewLineOnAttributes, it works fine.

[XML]$doc = get-content ".\input.xml"
$xwSettings = new-object System.Xml.XmlWriterSettings
$xwSettings.Indent = $true
$xwSettings.NewLineOnAttributes = $true
$xmlWriter = [Xml.XmlWriter]::Create("output.xml", $xwSettings)
$doc.Save($xmlWriter)
$xmlWriter.Dispose()

The above code works. Now the question is, is this documented? Oh yes in the remarks section.
Of course, the default value of the Indent property is false. The comparison looks as below after using the above code.


Happy formatting.

Tuesday, May 4, 2021

Uncomment section in XML file using C#.Net

Requirement 

As part of the installation, some XML fragments (eg: <authentication>) need to be uncommented in web.config file based on the environment,. This can be done either via PowerShell or C#.Net as this has to be triggered from MSI installation. Never during the runtime of the application.

Alternatives

We can either do string-based detection and replace it. Or use XML parser of .Net. Since the string parser is complex, let us stick with the .Net library to replace it.

Solution

The below code snippets is replacing the commented <authentication> tag in XML with its uncommented version.

Wednesday, December 15, 2010

Using XML literals in VB.Net with Linq

The XML literal support is the second thing I have seen special in Visual Basic which is really amazing.The first feature was the support for multiple indexer properties .XML literals helps us to create XElement by just writing plain XML in the VB.net code.This simple definition will not help anybody to learn what is XMl literals and their importance in dealing with xml.So let’s consider an example of XML serialization.
I have a class say Employee with 2 properties Name as string and ID as integer.The requirement is to get a XML representation of the employee collection.

Public Class Employee
Public Property Name As String
Public Property Id As Integer
End Class


If I pass a list of employee class I need out put like.


<Employees>
<Employee Name="Joy" Id="Joy" />
<Employee Name="George" Id="George" />
</Employees>


Here is the traditional code.


Private Function GetXMLTraditional(ByVal employees As List(Of Employee)) As String
Dim xEle As New XElement("Employees")
For Each emp In employees
Dim xEleEmp As New XElement("Employee")
xEleEmp.SetAttributeValue("Name", emp.Name)
xEleEmp.SetAttributeValue("Id", emp.Id)
xEle.Add(xEleEmp)
Next
Return xEle.ToString()
End Function


Look at the code which uses XML literals.


Private Function GetXMLUsingXMLLiterals(ByVal employees As List(Of Employee)) As String
Return <Employees>
<%= From emp In employees _
Select <Employee Name=<%= emp.Name %> Id=<%= emp.Name %>/>
%>
</Employees>.ToString
End Function



I don’t think I need to explain anything further about the advantages.If you are a ASP.Net developer you might already understood the use of “<%= %>”