Monday 24 January 2011

Escaping and Unescaping XML Literals

XML encoding is necessary if you have to save XML text in an XML document. If you don't escape special chars the XML to insert will become a part of the original XML DOM and not a value of a node.


Escaping the XML means basically replacing 5 chars with new values.


These replacements are:

<

->

& lt;

>

->

& gt;

"

->

& quot;

'

->

& apos;

&

->

& amp;

Here are 4 ways you can encode XML in C#:


1. String.Replace() 5 times

This is ugly but it works. Note that Replace("&", "&") has to be the first replace so we don't replace other already escaped &.

string xml = "it's my \"node\" & i like it";

encodedXml = xml.Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'");


2. System.Web.HttpUtility.HtmlEncode()

Used for encoding HTML, but HTML is a form of XML so we can use that too. Mostly used in ASP.NET apps. Note that HtmlEncode does NOT encode apostrophes ( ' ).

string xml = "it's my \"node\" & i like it";

string encodedXml = HttpUtility.HtmlEncode(xml);

// RESULT: <node>it's my "node" & i like it<node>


3. System.Security.SecurityElement.Escape()

In Windows Forms or Console apps I use this method. If nothing else it saves me including the System.Web reference in my projects and it encodes all 5 chars.


string xml = "it's my \"node\" & i like it";

string encodedXml = System.Security.SecurityElement.Escape(xml);


4. System.Xml.XmlTextWriter

Using XmlTextWriter you don't have to worry about escaping anything since it escapes the chars where needed. For example in the attributes it doesn't escape apostrophes, while in node values it doesn't escape apostrophes and qoutes.

string xml = "it's my \"node\" & i like it";

using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode))

{

xtw.WriteStartElement("xmlEncodeTest");

xtw.WriteAttributeString("testAttribute", xml);

xtw.WriteString(xml);

xtw.WriteEndElement();

}

5. Using Switch Case

public string XmlEncode(string nonXmlText)

{

StringBuilder builder = new StringBuilder();

Char[] originalChars = nonXmlText.ToCharArray();

for (int i = 0; i <>

{

switch ((byte)originalChars[i])

{

case 34:

case 38:

case 39:

case 60:

case 61:

case 62:

builder.Append("&#");

builder.Append(originalChars[i]);

builder.Append(";");

break;

default:

builder.Append(originalChars[i]);

break;

}

}

return builder.ToString();

}


Unescaping XML characters:

public static string UnescapeXml(string xmlString)

{

if (xmlString.Length > 0)

{

xmlString = xmlString.Replace("<", "<");

xmlString = xmlString.Replace(">", ">");

xmlString = xmlString.Replace("&", "&");

xmlString = xmlString.Replace(""", "\"");

xmlString = xmlString.Replace("'", "’");

xmlString = xmlString.Replace("'", "’");

}

return xmlString;

}

Wednesday 1 December 2010

Changing IP and Domain Using Batch Files

To change an IP Address, you can do it easily using the netsh command. However, to change the domain you need to have a tool called netdom.exe - Windows Domain Manager.

Installation of netdom.exe:
This tool gets installed along with the WindowsXP-KB838079-SupportTools-ENU, available atttp://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=49ae8576-9bb9-4126-9761-ba8011fabf38. You need to copy the netdom.exe file into the location from where you are executing the batch files. The netdom.exe will be present in after the installation of the above software.


To Change an IP address from a DHCP enabled to Static IP Address:

@ECHO OFF
set varip=10.45.132.20
set varsm=255.255.255.240
set vargw=172.23.41.1
set vardns1=172.23.41.1
REM ***** You don’t need to change anything below this line! ******
ECHO Setting IP Address and Subnet Mask
netsh int ip set address name = "Local Area Connection" source = static addr = %varip% mask = %varsm%
ECHO Setting Gateway
netsh int ip set address name = "Local Area Connection" gateway = %vargw% gwmetric = 1
ECHO Setting Primary DNS
netsh int ip set dns name = "Local Area Connection" source = static addr = %vardns1%

ECHO Here are the new settings for %computername%:
netsh int ip show config

netdom join %computername% /d:advgtic11.local /ud:ncruser1 /pd:Password1 /reboot:00
pause


Cheers

Show Folder Tree in Windows 7 Explorer

Windows by default does not auto expand the tree view when you navigate between folders. The result being you don’t see the usually tree hierarchy on the left pane which makes it quite painful if you want to go up a few levels or see the other parent folders.


Luckily there is a new Folder Option called Navigation Pane to switch them back on again. You need to press Alt + T or Organize –> Folder Search Options in Windows Explorer to get to the dialog.


Happy Browsing :-)

Cheers