Serialization and deserialization of Simple objects

Source: Internet
Author: User

The Code is as follows. Some of them use network resources for reference.

Although there are few codes, it takes a long time.

 

This class starts with a network interface. This interface

Yes, net webservice development. It is simple and convenient to transmit data using SOAP in xml format. Before getting the documentation and demo, I am deeply comforted by the fact that I have used several interfaces that are pieced together by rule strings to get data. This time I heard of a webservice, I thought I would not have to stare at the string all day.

 

When I get the network address, I directly go to the project and reference the web service. Then try to call the interface function. You can only find the method with only one string parameter. Because the data I want to transmit and obtain has more than 10 fields. I was wondering. I had to go over the document and then go over the demo... Finally, in the demo, find the correct solution: the so-called xml data transmission, originally only a string in xml format.

The mood at that time was not detailed, and two words ran into tears.

 

There is no way to do that. As usual, the string is converted into a class, and then the method is written to provide external calls. Although it is a string, but it is still a long xml format, let's treat it as an xml document, so we have the following code:

 

 

Code

Public class XmlObjConvert
{
/// <Summary>
/// Convert from object to xml string
/// </Summary>
/// <Param name = "obj"> </param>
/// <Returns> </returns>
Public static string SerializeXml (object obj)
{
String xml = string. Empty;
XmlSerializer xs = new XmlSerializer (obj. GetType ());
XmlWriterSettings settings = new XmlWriterSettings ()
{
Encoding = Encoding. UTF8
};
MemoryStream stream = new MemoryStream (); // If StringBuilder is used, the converted xml encoding is UTF16, and XmlWriterSettings does not work. You can only change the Stream encoding.
Using (XmlWriter writer = XmlWriter. Create (stream, settings ))
{
Xs. Serialize (writer, obj );
}
Return Encoding. UTF8.GetString (stream. toArray ()). trim (); // after being converted to an xml string, there will be a small space encoded as 65279, which only occupies 1 pixel.
}
/// <Summary>
/// Convert from xml to object
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "xml"> </param>
/// <Returns> </returns>
Public static T DeserializeXml <T> (string xml) where T: class
{
If (xml = null | xml = string. Empty)
{
Return default (T );
}
XmlSerializer xs = new XmlSerializer (typeof (T ));
XmlTextReader xtr = new XmlTextReader (new StringReader (xml ));
Object obj = xs. Deserialize (xtr );

Return obj as T;
}
}

 

There is something that has been tossing me for a long time in the above comments. "A small space code is 65279." The Space size is estimated to be 1 to 2 pixels. I don't know why it came out here. But it is because of this space. An exception is reported when the converted xml string is converted to an object again.

 

The data at the root level is invalid. Row 1, position 1.

Finally, this space was accidentally found during the adjustment.

I don't know how to use this space ......

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.