Csharp/CSharp Tutorial/Network/SOAP Serialization — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:20, 26 мая 2010
Содержание
Call Soap service
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.XPath;
public class CallSoap {
private const string soapNS = "http://schemas.xmlsoap.org/soap/envelope/";
private const string yourNS = "http://yourName.ru";
private static readonly Encoding encoding = Encoding.UTF8;
public static void Main(string [] args) {
MemoryStream stream = new MemoryStream( );
XmlTextWriter writer = new XmlTextWriter(stream,encoding);
writer.WriteStartDocument( );
writer.WriteStartElement("soap","Envelope",soapNS);
writer.WriteStartElement("Body",soapNS);
writer.WriteStartElement("GetNumberInStock",yourNS);
writer.WriteElementString("productCode","1111");
writer.WriteEndElement( );
writer.WriteEndElement( );
writer.WriteEndElement( );
writer.WriteEndDocument( );
writer.Flush( );
stream.Seek(0,SeekOrigin.Begin);
StreamReader reader = new StreamReader(stream);
string soap = reader.ReadToEnd( );
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://127.0.0.1/myQuery.asmx");
request.Method = "POST";
request.ContentType = "text/xml; charset=" + encoding.HeaderName;
request.ContentLength = soap.Length;
request.Headers["SOAPAction"] = "http://Product.ru/GetNumberInStock";
StreamWriter streamWriter = new StreamWriter(request.GetRequestStream( ));
streamWriter.Write(soap);
streamWriter.Flush( );
WebResponse response = request.GetResponse( );
Stream responseStream = response.GetResponseStream( );
XPathDocument document = new XPathDocument(responseStream);
XPathNavigator nav = document.CreateNavigator( );
XPathNodeIterator nodes = nav.Select("//Envelope/Body/GetNumberIn/GetNumberInResult");
Console.WriteLine(nodes.Current);
}
}
Invoke web service with Http Get
using System;
using System.IO;
using System.Net;
using System.Xml.XPath;
public class GetNumberInStockHttpGet {
public static void Main(string [] args) {
WebRequest request = WebRequest.Create("http://127.0.0.1/Query.asmx/GetNumber?pCode=1111");
request.Method = "GET";
WebResponse response = request.GetResponse( );
Stream stream = response.GetResponseStream( );
XPathDocument document = new XPathDocument(stream);
XPathNavigator nav = document.CreateNavigator( );
XPathNodeIterator nodes = nav.Select("//int");
Console.WriteLine(nodes.Current);
}
}
Serialization of an object list in SOAP
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
[Serializable]
public class MyElement
{
public MyElement(string name)
{
this.name = name;
this.cacheValue = 15;
}
public override string ToString()
{
return(String.Format("{0}: {1}", name, cacheValue));
}
string name;
[NonSerialized]
int cacheValue;
}
[Serializable]
public class MyElementList
{
public void Add(MyElement my)
{
row.Add(my);
}
public override string ToString()
{
string temp = null;
foreach (MyElement my in row)
temp += my.ToString() + "\n";
return(temp);
}
ArrayList row = new ArrayList();
}
class MainClass
{
public static void Main()
{
MyElementList row = new MyElementList();
row.Add(new MyElement("Gumby"));
row.Add(new MyElement("Pokey"));
Console.WriteLine("Initial value");
Console.WriteLine("{0}", row);
// write to SOAP (XML), read it back
Stream streamWrite = File.Create("MyElementList.xml");
SoapFormatter soapWrite = new SoapFormatter();
soapWrite.Serialize(streamWrite, row);
streamWrite.Close();
Stream streamRead = File.OpenRead("MyElementList.xml");
SoapFormatter soapRead = new SoapFormatter();
MyElementList rowSoap = (MyElementList) soapRead.Deserialize(streamRead);
streamRead.Close();
Console.WriteLine("Values after SOAP serialization");
Console.WriteLine("{0}", rowSoap);
}
}
Initial value Gumby: 15 Pokey: 15 Values after SOAP serialization Gumby: 0 Pokey: 0
Serialize object to SOAP message
using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
[Serializable]
public class MyElement
{
public MyElement(string name)
{
this.name = name;
this.cacheValue = 15;
}
public override string ToString()
{
return(String.Format("{0}: {1}", name, cacheValue));
}
string name;
[NonSerialized]
int cacheValue;
}
class MainClass
{
public static void Main()
{
MyElement ele = new MyElement("name");
Console.WriteLine("Initial value");
Console.WriteLine("{0}", ele);
// write to SOAP (XML), read it back
Stream streamWrite = File.Create("MyElement.xml");
SoapFormatter soapWrite = new SoapFormatter();
soapWrite.Serialize(streamWrite, ele);
streamWrite.Close();
Stream streamRead = File.OpenRead("MyElement.xml");
SoapFormatter soapRead = new SoapFormatter();
MyElement element = (MyElement) soapRead.Deserialize(streamRead);
streamRead.Close();
Console.WriteLine("Values after SOAP serialization");
Console.WriteLine("{0}", element);
}
}
Initial value name: 15 Values after SOAP serialization name: 0
Soap Custom Serialization
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
[Serializable]
class Employee: ISerializable
{
int id;
string name;
string address;
public Employee(int id, string name, string address)
{
this.id = id;
this.name = name;
this.address = address;
}
public override string ToString()
{
return(String.Format("{0} {1} {2}", id, name, address));
}
Employee(SerializationInfo info, StreamingContext content)
{
id = info.GetInt32("id");
name = info.GetString("name");
address = info.GetString("address");
}
// called to save the object data
public void GetObjectData(SerializationInfo info, StreamingContext content)
{
info.AddValue("id", id);
info.AddValue("name", name);
info.AddValue("address", address);
}
}
class MainClass
{
public static void Main()
{
Employee employee = new Employee(15, "F", "B");
Stream streamWrite = File.Create("emp.dat");
IFormatter writer = new SoapFormatter();
writer.Serialize(streamWrite, employee);
streamWrite.Close();
Stream streamRead = File.OpenRead("emp.dat");
IFormatter reader = new SoapFormatter();
employee = (Employee) reader.Deserialize(streamRead);
streamRead.Close();
Console.WriteLine("Employee: {0}", employee);
}
}
Employee: 15 F B