XML Web Services Type Marshaling

Description

This article illustrates that various data types can be passed to and returned from Web Service methods. Because the XML Web services implementation is built on top of the XML Serialization architecture, it supports more number of data types. Here I am going to show Web Service which returns String, Array, Class, integer, and Dataset.

Note: This Article Based on RC1 Version

Integer Type

//This WebMethod will take two integer Arguments and return
// result as int data type
[WebMethod]
public int Add (int A, int B)
{
return A+B;
}

String Type

//This WebMethod will take no Arguments and return
// result as string data type
[WebMethod]
public string HelloWorld()
{
return "Hello Seenivasaragavan";
}

Class Type

//This WebMethod will take no Arguments and return
// result as class data type.
[WebMethod]
public Person GetMyRecord ()
{
Person s=
new Person ();
s.Name ="S.Siddarth Sundarram";
s.age ="1.5";
s.Address ="1036 san Jacinto drive";
return s;
}

Integer Array

//This WebMethod will take no Arguments and return
// result as int Array data type
[WebMethod]
public int[] MyMarks()
{
int i;
int [] Marks = new int [5];
for (i=0;i<5;i++)
{
Marks[i]=3*20;
}
return Marks;
}

Dataset

//This WebMethod will take no Arguments and return
// result as Dataset data type
//In this WebMethod I am reading windows eventlog and //constructing a XML and writing to one file from that
//XML to Dataset
[WebMethod()]
public DataSet ReadEventLog()
{
DataSet ds =
new DataSet(); // Creating DataSet objects
EventLog aLog = new EventLog();
AppLog.Log = "Application";
//Reading only Application logs
AppLog.MachineName = "."; // Local machine
string str="<?xml version='1.0' ?>";
str=str+"<MSG>";
foreach (EventLogEntry entry in AppLog.Entries)
{
str = str +"<Msg>";
str=str + "<Type>"+entry.EntryType+"</Type>";
str =str+"<Date>"+entry.TimeWritten.ToString()+"</Date>";
str =str +"<Source>"+entry.Source+"</Source>";
str =str+"<Description>"+entry.Message.ToString()+"</Description>";
str=str+"</Msg>";
}
str=str+"</MSG>";
FileStream fs =
new FileStream("c:\\Raga.xml" , FileMode.Create, FileAccess.ReadWrite);
//Now let's put some text into the file using the StreamWriter
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(str);
sw.Flush();
sw.Close();
ds.ReadXml("c:\\raga.xml");
return ds;
}

This is Output of Windows EventLog which returns from ReadEventLog WebMethod as dataset

WebServiceTypeMarshalingImg1.jpg