Wednesday, June 09, 2004

[Tip] Encoding and decoding special characters to XML friendly equivalents:

There are often occasions when you need to use special characters and have them represented as valid XML element names.
As we already know, characters such as blank spaces, &, <, > etc. cannot be represented as valid XML element or attribute
names just the way they are. They need to be encoded or escaped into characters that are accepted as valid XML.

The XmlConvert class provides an easy way to do this through the EncodeName() and DecodeName() methods.

Let's take a look at an example:

using System;
using System.Xml;

class Product
{
[STAThread]
static void Main(string[] args)
{
XmlTextWriter xmlTextWriter = null;
try
{
xmlTextWriter = new XmlTextWriter(@"c:\products.xml", null);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("Products");
xmlTextWriter.WriteStartElement("Product");
xmlTextWriter.WriteAttributeString("SKU", "123");
xmlTextWriter.WriteAttributeString("Product", "ChocoRaisin Supreme");
xmlTextWriter.WriteElementString("Redorder Level", "200");
xmlTextWriter.WriteElementString("Notifications & Alerts", "Reorder@AcmeIcecreams.com");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
}
finally
{
if(xmlTextWriter != null)
{
xmlTextWriter.Flush();
xmlTextWriter.Close();
}
}
}
}

Notice that the Reorder Level and the Notifications & Alerts element names contain invalid XML characters (space and ampersand
respectively) that causes malformed XML to be written to the Products.xml file.

As shown below, to correct this, use the XmlConvert.EncodeName() to encode such special characters into XML friendly equivalents:

xmlTextWriter.WriteElementString(XmlConvert.EncodeName("Redorder Level"), "200");
xmlTextWriter.WriteElementString(XmlConvert.EncodeName("Notifications & Alerts"), "Reorder@AcmeIcecreams.com");

This causes the following encoded XML to be written to Products.xml:



200
Reorder@AcmeIcecreams.com



Conversely, use XmlConvert.DecodeName() to convert the encoded characters back to their original form.

Tuesday, June 01, 2004

COMMA SEPARATED DATABASE INSERT
=================================

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sprocCreateIndustryImageAssoc]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sprocCreateIndustryImageAssoc]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO






CREATE PROC sprocCreateIndustryImageAssoc
(
@ImageList varchar(5000),
@IndustryID int
)
AS
BEGIN

Begin Tran

SET NOCOUNT ON

IF @ImageList = ''
BEGIN
Delete From tblAFDImageIndustryAssoc where IndustryID=@IndustryID
commit tran
RETURN
END


DECLARE @ImageID int, @Pos int

SET @ImageList = LTRIM(RTRIM(@ImageList))+ ','
SET @Pos = CHARINDEX(',', @ImageList, 1)

IF REPLACE(@ImageList, ',', '') <> ''
BEGIN
Delete From tblAFDImageIndustryAssoc where IndustryID=@IndustryID
WHILE @Pos > 0
BEGIN
SET @ImageID = LTRIM(RTRIM(LEFT(@ImageList, @Pos - 1)))
IF @ImageID <> ''
BEGIN

INSERT INTO tblAFDImageIndustryAssoc (ImageID,IndustryID) VALUES (CAST(@ImageID AS int),@IndustryID) --conversion of ImageID to Int
if @@Error > 0
begin
rollback tran
return -1
end
END
SET @ImageList = RIGHT(@ImageList, LEN(@ImageList) - @Pos)
SET @Pos = CHARINDEX(',', @ImageList, 1)

END
END

--SELECT ImageID,IndustryID FROM tblAFDImageIndustryAssoc



Commit Tran
return 0
END






GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO