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

Monday, July 15, 2013

how to compare two xml files in asp.net using c#

Title:How to compare XML files in Asp.net using C#

Description:

Now i would like to give an examples on Comparing of XML documents using C# code.Asp.net provide an algorithm which is "hash algorithm",can be used for encryption.So based on those methods i will do the comparison of files In the below method i have use two parameters which indicates the path of XML files

Examples:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;

private void CompareXMLDocumnet(string ActualDocumnet,string GenratedDocument)
{
if (File.Exists(ActualDocumnet) && File.Exists(GenratedDocument))
{
HashAlgorithm HA = HashAlgorithm.Create();
FileStream XmlFstream1 = new FileStream(ActualDocumnet, FileMode.Open);
FileStream XmlFstream2 = new FileStream(GenratedDocument, FileMode.Open);
byte[] Actual_Xmlhash1;
byte[] Genrated_Xmlhash2;
Actual_Xmlhash1= HA.ComputeHash(XmlFstream1);
Genrated_Xmlhash2= HA.ComputeHash(XmlFstream2);
XmlFstream1.Close();
XmlFstream2.Close();
if (Convert.ToBase64String(Actual_Xmlhash1) == Convert.ToBase64String(Genrated_Xmlhash2))
{
Response.Write(" similar="">
}
else
{
throw new Exception("Files are different");
}
}
}

Wednesday, April 25, 2012

How to Get all xml Items using xslt

Before doing this task i don't have an idea about xslt .Xslt is easy to learn and  to develop. Here i will show how to get the all XML items and display the items as list of anchor at client side view.For this i will use for loop to get the XML data

<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform">  
    <xsl:template match="/">
        <xsl:for-each select="List/Item">           
                <a>
                    <xsl:attribute name="href">
                        <xsl:text>#</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="title">
                        <xsl:text>Title</xsl:text>
                    </xsl:attribute>
                    <xsl:attribute name="target">
                        <xsl:text>blank</xsl:text>
                    </xsl:attribute>                   
                    <img>
                        <xsl:attribute name="src">
                            <xsl:value-of select="href"/>
                        </xsl:attribute>
                        <xsl:attribute  name="alt">
                            <xsl:value-of select="Title"/>
                        </xsl:attribute>
                        <xsl:attribute  name="class">
                            <xsl:text>slider_img</xsl:text>
                        </xsl:attribute>
                    </img>
                    <span>
                        <b>
                            <xsl:value-of select="Title"/>
                        </b>                       
                        </span>
                </a>               
        </xsl:for-each>    </xsl:template>  
</xsl:stylesheet>

Thursday, March 22, 2012

How to update the XML file in asp.net

Title:How to read and update XML in asp.net using c#

Description: As per asp.net we have specific number of data source controls.Among these i have used XML data source control for my application.So now i would like to describe and explain about CRUD operations on XML file.As initial we have to get the data source path and read the data from the file .

Example:In the below example we have create a instance for XML document and load  from the specified path.Then i will get the all sales order id's using node list and iterate through the complete document.Mean while we are getting the sales Order customer title and update the name as per data
using System;
using System.Collections.Generic;
using System.Data;
using System.XML;

private void ReadAndUpdateXml_Click(object sender, EventArgs e)
{
XmlDocument newXmldoc= new XmlDocument();
newXmldoc.Load(Server.MapPath("~/XMLFiles/salesOrder.xml"));
XmlNodeList nodeList = newXmldoc.SelectNodes("//Orders/OrderId");
int i = 1;
foreach (XmlNode updatedNode in nodeList)
{
XmlNode updatedNode = newXmldoc.SelectSingleNode("//Orders/OrderId[position()='" +i + "']");
string salesOrderId = updatedNode.SelectSingleNode("ID").InnerText;
updatedNode.SelectSingleNode("Title").InnerText ="Specified Customer";
XmlNode UpdateTitle = newXmldoc.CreateNode(XmlNodeType.Element, "MetaTitle", null);
UpdateTitle.InnerText = "Desired Customer";
updateNode.AppendChild(UpdateTitle);
i++;
}
newXmldoc.Save(Server.MapPath("~/XMLFiles/salesOrder.xml"));
}

Friday, November 25, 2011

XML data into Gridview in asp.net

Title:how to bind XML data in grid view in asp.net using C#.net

Description:
We have seen different examples on  how to bind data to Grid view in asp.net ,Import XML data to Grid View,bind data table to Grid view.Now i would like show how to bind the XML data to grid view.Before going to start  you may think we have to use XML classes to read the data,But we will not use any XML classes .In asp.net Data set has property to read the XML data which is Readxml().
XML Data:
<?xml version="1.0" encoding="utf-8" ?>
<Orders>
<Order>
<OrderId>1</OrderId>
<OrderName>Asp.net</OrderName>
<Phone>0000000000</Phone>
<Address>Hyd</Address>
<Amount>100</Amount>
</Order>
<Order>
<OrderId>2</OrderId>
<OrderName>Sharepoint</OrderName>
<Phone>1111111111</Phone>
<Address>USA</Address>
<Amount>200</Amount>
</Order>
<Order>
<OrderId>3</OrderId>
<OrderName>Jquery</OrderName>
<Phone>2222222222</Phone>
<Address>UK</Address>
<Amount>200</Amount>
</Order>
<Order>
<OrderId>4</OrderId>
<OrderName>Mvc</OrderName>
<Phone>4444444444</Phone>
<Address>AUS</Address>
<Amount>300</Amount>
</Order>
</Orders>

Aspx Page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Bind XML data to Gridview in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GVdynamicXML" runat="server" AutoGenerateColumns="False"
>
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID"
 SortExpression="OrderID" />
<asp:BoundField DataField="OrderName" HeaderText="OrderName"
 SortExpression="OrderName" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="Amount" HeaderText="Amount"
SortExpression="Amount" />
</Columns>
</asp:GridView>
</form>
</body>
</html>

Code behind:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _XMLbindGridviewDefault3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(Server.MapPath("~/XMLFile.xml"));
GVdynamicXML.DataSource = myDataSet;
GVdynamicXML.DataBind();
}
}
}
We should give a specific path of XML file to read the data using Readxml().Then bind the data  to grid view "gd_view"

Bel