ASp.net c# sql

February 17, 2011

Generate Structured Class from xml file

Filed under: asp.net c#, Asp.net, c#, mssql, XML, xml — Tags: , , , , , — Admin @ 1:36 pm

Tired of writing .cs class?

we can generate it see example below…

I created a test.xml file

<test>

<field1>hi</field1>

<field2>bye</field2>

</test>

Step1:

Run visual studio command promt

step2: move to the folder where test.xml is saved and execute below command

xsd test.xml

Above command will generate xsd file based on test.xml

and then execute below command to generate the .cs file

xsd /c test.xsd

Done.

You can easily generate .cs file from xml file.

 

 

Now

 

June 2, 2008

Remove Special Character from XML string using SQL Server

Create a function in MSSQL to remove all special characters from XML.

ALTER FUNCTION [dbo].[RemoveSpChar]
(
— Add the parameters for the function here
@sInput varchar(MAX)=”
)
RETURNS varchar(MAX)
AS
BEGIN
— Declare the return variable here
DECLARE @sOutput Varchar(MAX),
@iIndex int,
@iLength int,
@sChar varchar(1),
@iASCII int,
@iLen int,
@iRem int

set @sInput= ltrim(rtrim(@sInput))
set @iLength = len(@sInput)
set @iIndex =1
set @sOutput=”

while @iIndex <= @iLength
begin
set @sChar=substring(@sInput,@iIndex,1)
set @iASCII=ascii(@sChar)

if ((@iASCII>=48 and @iASCII<=57) or (@iASCII>=65 and @iASCII<=90) or (@iASCII>=97 and @iASCII<=122) )
set @sOutput=@sOutput+@sChar
–return @sChar + ‘ – ‘ + convert(varchar,@iASCII)
set @iIndex =@iIndex +1
end

if len(@sOutput)>17
set @sOutput=substring(@sOutput,1,17)
else if len(@sOutput)<6
begin
set @iLen=len(@sOutput)
set @iRem=6-@iLen

set @sOutput=substring(@sOutput + replicate(‘0’,@iRem),1,6)

end

— Return the result of the function
RETURN @sOutput

END

May 7, 2008

– Fetch XML (xmldatadocument) from database

Filed under: asp.net c#, sql, xml — Tags: , , , , — Admin @ 1:19 pm

This post shows you how to return XmlDataDocument from the database.
This helps you to fetch the xml data created in sql and use in asp.net.
If you are familiar how to get xml data from sql then this post helps you to fetch the xml data in XmlDataDocument.

//Code Starts Here

// http://helpindotnet.blogspot.com/
// https://helpindotnet.wordpress.com

public XmlDataDocument getauditnames(string prefixText, int count)
{

XmlDataDocument xml = new XmlDataDocument();
SqlConnection con = new SqlConnection(” <<Connection String >>”);
SqlCommand com = new SqlCommand(“<<Query>> For XML PATH (‘Path’), root(‘Root’)”, con);
//The key to this step is the FOR XML PATH(###), ROOT(###) part. This tells SQL Server to return XML with each row having the element name //”Path” of and the root of the XML document to be ROOT
con.Open();
XmlReader xdr = com.ExecuteXmlReader();
xml.Load(xdr);
xdr.Close();
con.Close();
return xml;
}
//Code Ends Here

May 6, 2008

– Encode Decode XML Name

Filed under: xml — Tags: , , , , — Admin @ 8:52 am

<span style=”font-weight:bold;”>While coding with XML Files, Sometimes you need to Encode and Decode the Xml Name.
Here is the simplest method to encode the name XmlConvert.EncodeName(string).
</span>

using System.Xml;
namespace ConsoleApplication1
{
class classEncodeDecodeXMLName
{
static void encodedecode()
{
// Encode and decode a name with spaces.
Console.WriteLine(XmlConvert.ToByte(“Some Name”));
string name1 = XmlConvert.EncodeName(“Order Detail”);
Console.WriteLine(“Encoded name: ” + name1);
Console.WriteLine(“Decoded name: ” + XmlConvert.DecodeName(name1));

// Encode and decode a local name.
string name2 = XmlConvert.EncodeLocalName(“a:book”);
Console.WriteLine(“Encoded local name: ” + name2);
Console.WriteLine(“Decoded local name: ” + XmlConvert.DecodeName(name2));
}
public static void Main()
{
encodedecode();
}
}
}

May 2, 2008

– Validate XML against Schema

Filed under: xml — Tags: , , — Admin @ 6:34 am

Here I will show you how to “Validate Xml against the provided Schema xsd”.
This is the simplest method to validate the xml from the given Schema xsd
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Data;
using System.Web;
namespace ConsoleApplication1
{
class clsValidateXml
{
public static string ErrorMessage=””;
static void validatexml()
{
//Physical path of the XML File
string XmlFilePath = @”c:\Test.xml”;
//Physical path of the XSD File
string XSDFilePath = @”c:\Test.xsd”;

XmlDocument ObjXmlDocument = new XmlDocument();
ObjXmlDocument.Load(XmlFilePath);

XmlNodeList list = null;
list = ObjXmlDocument.GetElementsByTagName(“ProductPageUrl”);
string tags = null;
foreach (XmlNode node1 in list)
{
//do something with tags
}
ObjXmlDocument.Save(@”c:\final.xml”);

try
{
// Load the schema definition
XmlReaderSettings ObjXMLSettings = new XmlReaderSettings();
ObjXMLSettings.ValidationType = ValidationType.Schema;
ObjXMLSettings.ValidationEventHandler += new ValidationEventHandler(ObjXMLSettings_ValidationEventHandler);
ObjXMLSettings.Schemas.Add(null, XmlReader.Create(XSDFilePath));

XmlNodeReader ObjXmlNodeReader = new XmlNodeReader(ObjXmlDocument);
System.Xml.XmlReader ObjXMLReader,asd = null;
ObjXMLReader = XmlReader.Create(ObjXmlNodeReader, ObjXMLSettings);

// If there XML file does not match the XSD file
//then Validation event handler is fired in this statement
while (ObjXMLReader.Read())
{
}
if (!String.IsNullOrEmpty(ErrorMessage.ToString()))
{
Console.WriteLine(ErrorMessage.ToString());
}
}

catch (XmlException ObjXMLException)
{
Console.WriteLine(“Not a Well Formed XML Document” + ObjXMLException.Message);
}
}
public static void ObjXMLSettings_ValidationEventHandler(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Error)
{
ErrorMessage+=args.Message;
}
}
public static void Main()
{
validatexml();
}
}
}

In the Next post I will Show you how to encode and decode the xml Name……

Create a free website or blog at WordPress.com.