Thursday, June 10, 2010

Remove duplicate nodes from XML using XSLT

HI All!

Its been a long time since my last post. I finally got something which I feel need to be posted else I will have a hard

time finding it later.

Ok, one of my recent quests was how to remove duplicate nodes from an xml like below


<?xml version="1.0" encoding="utf-16"?>
<country>
<state>
<count>19</count>
<city>
<street name="id" value="1" />
<street name="name" value="MGROAD" />
</city>
<city>

<street name="id" value="2" />
<street name="name" value="SAVOY" />

</city>
<city>

<street name="id" value="3" />
<street name="name" value="MTBATTENROAD" />

</city>
</state>
</country>


Now if we want to transform this XML into another XML without the repeating
<street id="1" name="MGROAD" /> tag then the xsl to be used is thus :


<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>


<xsl:template match ="count">
</xsl:template>

<xsl:template match="city">

<xsl:copy>
<xsl:apply-templates select="@*"/>
<street>
<xsl:attribute name ="id">
<xsl:value-of select ="current()/self::node()/street[@name='id']/@value"/>
</xsl:attribute>
<xsl:attribute name ="name">
<xsl:value-of select ="current()/self::node()/attribute[@name='name']/@value"/>
</xsl:attribute>
</attribute>
</xsl:copy>


</xsl:template>

</xsl:stylesheet>



Transform this using XSLT

XmlDocument stripper = new XmlDocument();
stripper.Load(Server.MapPath("~/App_Data/transform.xsl")); //Compile here
StringWriter output = new StringWriter();
XslCompiledTransform emptyNodeRemover = new XslCompiledTransform();
emptyNodeRemover.Load(stripper); //Port output to string
output = new StringWriter();
XmlDocument source = new XmlDocument();

source.Load(Server.MapPath("~/App_Data/response.xml"));

emptyNodeRemover.Transform(new XmlNodeReader(source), null, output);
output.Flush(); //Reload source with emptied nodes
source.LoadXml(output.ToString());


The above xsl also specifies to restructure the xml file into


<?xml version="1.0" encoding="utf-16"?>
<country>
<state>
<count>19</count>
<city>
<street id="1" name="MGROAD" />
</city>
<city>
<street id="2" name="SAVOY" />
</city>
<city>
<street id="3" name="MTBATTENROAD" />
</city>
</state>
</country>


Now this transformed XML can be fed into a telerik RadGrid successfully as it is the format it accepts.

Cheers!

Remove duplicate nodes from an XML using XSL

HI All!

Its been a long time since my last post. I finally got something which I feel need to be posted else I will have a hard time finding it later.

Ok, one of my recent quests was how to remove duplicate nodes from an xml like below





19




















Now if we want to transform this XML into another XML without the repeating
" tag then the xsl to be used is thus :



































Transform this using XSLT

XmlDocument stripper = new XmlDocument();
stripper.Load(Server.MapPath("~/App_Data/transform.xsl")); //Compile here
StringWriter output = new StringWriter();
XslCompiledTransform emptyNodeRemover = new XslCompiledTransform();
emptyNodeRemover.Load(stripper); //Port output to string
output = new StringWriter();
XmlDocument source = new XmlDocument();

source.Load(Server.MapPath("~/App_Data/response.xml"));

emptyNodeRemover.Transform(new XmlNodeReader(source), null, output);
output.Flush(); //Reload source with emptied nodes
source.LoadXml(output.ToString());


The above xsl also specifies to restructure the xml file into





19













Now this transformed XML can be fed into a telerik RadGrid successfully as it is the format it accepts.

Cheers!

Friday, October 24, 2008

How to change localhost name WCF

Goto Default Web Site in Internet Information Services and right click to open the Properties Tab . Click on Advanced and change the Host Header name . Save and Close. Open ur soultion project and add the existing web site pointing to the WCF service . The service will be added as
http://newhostheadername/foldername/servicename.svc

To run this service , you have to goto c:\winnt\system32\drivers\etc\host file and edit the host name for 127.0.0.1 change localhost to the newhostheadername . save build the web site

Finally add a cherry to the pie do an IISRESET !!!!!

You will be able to browse the WCF service as

http://newhostheadername/foldername/servicename.svc
http://msmvps.com/blogs/bernard/archive/2004/07/29/10855.aspx


References : http://www.developersdex.com/asp/message.asp?p=4100&r=6155011

Tuesday, October 14, 2008

How to Install MSMQ

Hi All ,

Most of you would have tried installing MSMQ and faced with a common error
"The MSMQ service could not be started " . Well, the MSMQ service depends on the NTLM Security Support Provider Service . You got to set that to Manual and start it . It also depends on Distribution Transaction Coordinator (DTC) . This service needs to be started too . Sometimes when you start this service you will be faced with an error with error code 1073737712
For resolving this please follow the steps given in the link below

http://windowsitpro.com/article/articleid/85807/jsi-tip-10458-when-you-start-the-distributed-transaction-coordinator-service-in-window-xp-or-windows-server-2003-you-receive-error--1073737712.html

Once these two services are up and running , you can select Message Queueing from the Add/ Remove Windows Components Box and click on Install .

Regards

Wednesday, September 3, 2008

Delete all Items inside a list

foreach (SPWeb childweb in web.Webs)
{
childweb.AllowUnsafeUpdates = true;
SPList PagesLIST = childweb.Lists["Pages"];
if (PagesLIST.Items.Count != 0)
{
for (int p = 0; p < PagesLIST.Items.Count; p++)
{
if (p == PagesLIST.Items.Count)
{ p = -1; continue; }
PagesLIST.Items[p].Delete();
}
}
PagesLIST.Update();

Monday, August 25, 2008

To get a dashed horizontal line

<hr style="height :0px; color:Black; border-bottom: 1px dashed #000; " />

Thursday, August 21, 2008

Get Previous Page Form Values

To get the previous page form element values follow the steps below

Parent page do a Server.Transfer instead of Response.Redirect

Server.Transfer("ChildPage.aspx", true);

true for preserving the form element values

In the ChildPage.aspx access the values from ParentPage.aspx like thus

((TextBox)(PreviousPage.FindControl("TextBox1"))).Text;