When I use pObject.GetType() instead of typeof() then my Serialization function not working.
actually in Serialization function parameter(this is my function syntax :-
public String SerializeToXML(Object pObject)), I am taking my url response before passing response, I convert my response as string array then pass to Serialization function. I am mentioning my Serialization function code.
abamboria
Member
145 Points
88 Posts
GetType() function not work for me???
Jan 08, 2013 09:52 AM|LINK
Hello,
When I use pObject.GetType() instead of typeof() then my Serialization function not working.
actually in Serialization function parameter(this is my function syntax :- public String SerializeToXML(Object pObject)), I am taking my url response before passing response, I convert my response as string array then pass to Serialization function. I am mentioning my Serialization function code.
public String SerializeToXML(Object pObject) { try { String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(pObject.GetType()); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, pObject); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } catch (Exception e) { throw e; } }Mark as Answer:- if the post helped you...
urenjoy
Star
12027 Points
1807 Posts
Re: GetType() function not work for me???
Jan 08, 2013 10:09 AM|LINK
The method looks fine as it is used in another thread also. Can you put code what you are passing and what is the expected result?
abamboria
Member
145 Points
88 Posts
Re: GetType() function not work for me???
Jan 08, 2013 10:17 AM|LINK
Thanks for reply. I am post my full code.
using System; using System.Xml.Serialization; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Net; using System.IO; using System.Xml; using System.Xml.Linq; using System.Security.Cryptography; using System.Text; using System.Web.Services; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { string city = dl_city.SelectedValue; string room = Dl_room.SelectedValue; string startdate = txtFrom.Text; string enddate = txtTo.Text; int room1 = Convert.ToInt16(room); var fromdate = startdate.ToString(); var todate = enddate.ToString(); string service = "http://api.ean.com/ean-services/rs/hotel/"; string version = "v3/"; string method = "list/"; string otherElemntsStr = "&cid=411931&minorRev=[12]&customerUserAgent=[hotel]&locale=en_US¤cyCode=INR"; string apiKey = "tzyw4x2zspckjayrbjekb397"; //string secret = "XCzhymKa"; string sig = "a6f828b696ae6a9f7c742b34538259b0"; string url = service + version + method + "?apiKey=" + apiKey + "&sig=" + sig + otherElemntsStr; // + "&hotelIdList=" + hotelId var poststring = String.Format("room1={0}&fromdat={1}&todate={2}&city={3}", room1, fromdate, todate, city); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; StreamWriter requestWriter = new StreamWriter(request.GetRequestStream()); requestWriter.Write(poststring); string fullurl = service + version + method + "?apiKey=" + apiKey + "&sig=" + sig + "&otherElemntsStr="+ otherElemntsStr + "&poststring=" + poststring; requestWriter.Close(); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { StreamReader responsereader = new StreamReader(response.GetResponseStream()); string responsedata = responsereader.ReadToEnd(); string[] rdata = responsedata.Select(c => c.ToString()).ToArray(); ; hoteltype harray = new hoteltype(); harray.SerializeToXML(rdata); responsereader.Close(); request.GetResponse().Close(); } } catch (WebException ex) { if (ex.Response == null) throw new NullReferenceException("WebException response"); throw ex; } } [XmlType("hoteltype")] public class hoteltype { public hoteltype() { } [XmlElement("hotelId")] public int hotelId { get { return hotelId; } set{ hotelId = value; } } [XmlElement("name")] public string name { get { return name; } set { name = value; } } [XmlElement("address")] public string address { get { return address; } set { address = value; } } [XmlElement("postalCode")] public int postalCode { get { return postalCode; } set { postalCode = value; } } [XmlElement("countryCode")] public string countryCode { get { return countryCode; } set { countryCode = value; } } private String UTF8ByteArrayToString(Byte[] characters) { UTF8Encoding encoding = new UTF8Encoding(); String constructedString = encoding.GetString(characters); return (constructedString); } public String SerializeToXML(Object htype) { try { String XmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(htype.GetType()); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter,htype); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray()); return XmlizedString; } catch (Exception e) { throw e; } } } }Mark as Answer:- if the post helped you...
abamboria
Member
145 Points
88 Posts
Re: GetType() function not work for me???
Jan 09, 2013 06:59 AM|LINK
Hey urenjoy,
Please response, I Post my code.
Mark as Answer:- if the post helped you...