Wednesday, November 16, 2011

xml DeSerialization xsd.exe

good article
-------

http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserialization-entity-classes-with-xsd-exe.aspx

below is simple steps using VS 2010

1) open XML file that you want to deserialize in VS 2010
2) from VS 2010 menu > XML > Create Schema
3) It will create schema in you project save somewhere this .xsd file (in project)
4) go to VS command prompt
5) change path to your .xsd file (where you save above .xsd file)
6) type : xsd myschemafile.xsd /c (must you /c)
7) it will generate myschemafile.cs file in same location
8) type following code

using (FileStream xmlStream = new FileStream(@"C:\book.xml", FileMode.Open))
{
using (XmlReader xmlReader = XmlReader.Create(xmlStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(catalog));
catalog deserializedStudents = serializer.Deserialize(xmlReader) as catalog;
foreach (var b in deserializedStudents.book )
{
Console.WriteLine("author : {0}", b.author);
Console.WriteLine("description : {0}", b.description);
Console.WriteLine("genre : {0}", b.genre);
Console.WriteLine("price : {0}", b.price);
Console.WriteLine("");
}
}
}



generate classes from XSD file

To generate the classes from XSD, run the following on the command line
LinqToXsd.exe /Myschema.xsd

Note: download above exe from codeplex
http://linqtoxsd.codeplex.com/

serialization - Deserialization example

public class MySampleClass
{
//class fields
public string mystring = "Hello World";
public int myint = 1234;
public string[] mystrarray = new string[4];
private int myprivateint = 4321;

//public property
private string _mynameproperty;
public string MyNameProperty
{
get { return _mynameproperty; }
set { _mynameproperty = value; }
}
//constructor
public MySampleClass()
{ }
//public method
public string MyMethod()
{
return _mynameproperty;
}
}


public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
//Serialization exmaple
//serialization is the process of saving class member data into XML
//only public properties and fields can be serialized
MySampleClass objmysampleClass = new MySampleClass();
objmysampleClass.MyNameProperty = "This is Prashant Rathod";
objmysampleClass.mystrarray[0] = "adfsasf";
objmysampleClass.mystrarray[1] = "546345";
objmysampleClass.mystrarray[2] = "uoafas";
objmysampleClass.mystrarray[3] = "34672";

XmlSerializer myxmlserializer = new XmlSerializer(typeof(MySampleClass));
StreamWriter mystreamwriter = new StreamWriter(@"c:\MysampleSerializeXMLfile.xml");
myxmlserializer.Serialize(mystreamwriter, objmysampleClass);
mystreamwriter.Close();
//Deserialization is the reverse process – loading class member data from an XML document
MySampleClass objfromXMLfile = new MySampleClass();
XmlSerializer myserializer = new XmlSerializer(typeof(MySampleClass));
FileStream filestream = new FileStream(@"c:\MysampleSerializeXMLfile.xml", FileMode.Open);
objfromXMLfile = (MySampleClass)myserializer.Deserialize(filestream);

Console.WriteLine(objfromXMLfile.mystring);
Console.WriteLine(objfromXMLfile.MyNameProperty);
Console.WriteLine(objfromXMLfile.myint);
Console.WriteLine(objfromXMLfile.mystrarray);

}
}

Monday, November 14, 2011

reading values from xml datatype in sql database

declare @test xml
DECLARE @ProvPayNoID int
set @test = '
'

SET @ProvPayNoID = @test.value('(/Fields/Field/@Value)[3]', 'int' )
SELECT @ProvPayNoID

declare @providerpayno int

select top 100 convert(xml,ExtraProperties).value('(/Fields/Field/@Value)[1]', 'int' ) as provpayno
from dbm.MedicalClaimsStaging
where BatchId='e5fff885-91bc-40a7-9350-5d176ca2fdfa'


declare @myDoc xml
DECLARE @ProdID int
set @myDoc = '
1 year parts and labor
3 year parts and labor extended maintenance is available
'
SET @ProdID = @myDoc.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )
SELECT @ProdID










'

SET @ProvPayNoID = @test.value('(/Fields/Field/@Value)[3]', 'int' )
SELECT @ProvPayNoID

declare @providerpayno int

select top 100 convert(xml,ExtraProperties).value('(/Fields/Field/@Value)[1]', 'int' ) as provpayno
from mytable



declare @myDoc xml
DECLARE @ProdID int
set @myDoc = '


1 year parts and labor
3 year parts and labor extended maintenance is available


'
SET @ProdID = @myDoc.value('(/Root/ProductDescription/@ProductID)[1]', 'int' )
SELECT @ProdID

Sunday, November 6, 2011

XML to XSD error

Error: Cannot have more than one type of file as input.

because the parameter sequence is not correct.

Parameter should be
nevigate to folder that has xml file

cd c:\xmltoxsdtest
type: XSD myxmlfile.xml
hit enter

create XSD file from XML file

using microsoft XSD.exe tool
1) go to command line (.Net command line)
2) cd path to where your xml file is located (cd c:\myxsdmapping)
3) type "XSD myxmlfile.xml
4) that it -- very simple