XML is passed between Java and. NET, and XML is one of the most up-front?

Source: Internet
Author: User

A recent project is that Java provides webservice for. NET calls. The parameters are in XML format.
The first problem encountered:
1). NET this side uses the XmlSerializer way to serialize objects to each other. The other side in the local debugging can be received, but deployed to the local or server will not be able to receive, specifically <?xml in front of more?
Suspicion is Java's coding problem, and has been to help them solve. No solution, they use the Java FXT Framework, the received string is that.
2) that can only be solved by the encoding problem of. NET, in plain text XML format, Java over there can be normal reception.
3)

   XmlSerializer serializer = new XmlSerializer (typeof (T));            using (System.IO.MemoryStream stream = new System.IO.MemoryStream ())            {                XmlWriter writer = new  XmlTextWriter (Stream, Encoding.UTF8);                System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces ();                Ns. Add ("", "");//Do not output xmlns                serializer. Serialize (writer, obj, NS);                Stream. Position = 0;                byte[] buf = new Byte[stream. Length];                Stream. Read (buf, 0, buf. Length);                result = Convert.tobase64string (BUF);                result = System.Text.Encoding.UTF8.GetString (BUF);            }

Change the above

XmlTextWriter encoding for default or Gb312,java side can receive normally. However, the XML format encoding becomes gb2312 and does not meet the requirements.
And then I saw http://blog.csdn.net/dancefire/article/details/1912345 found.

See "This time the output is normal." It is important to note that the encoding line, which is set encoding do not output the BOM, otherwise the generated string will be a few bytes before the byte Order. "

For BOM, please refer to:

Byte Order Mark (BOM) FAQ
Http://unicode.org/faq/utf_bom.html#BOM

Byte-order mark (Wikipedia)
Http://en.wikipedia.org/wiki/Byte_Order_Mark


The reason for this is the problem I am encountering now.
According to his method, the problem is solved.
The code is as follows:
using (System.IO.MemoryStream stream = new System.IO.MemoryStream ())            {                xmlwritersettings setting = new XmlWriterSettings ();                Setting. Encoding = new UTF8Encoding (false);                Setting. Indent = true;                XmlWriter writer =xmlwriter.create (stream, setting);                System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces ();                Ns. Add ("", "");//Do not output xmlns                serializer. Serialize (writer, obj, NS);                Stream. Position = 0;                byte[] buf = new Byte[stream. Length];                Stream. Read (buf, 0, buf. Length);                result = Convert.tobase64string (BUF);                result = System.Text.Encoding.UTF8.GetString (BUF);                            }

  

XML is passed between Java and. NET, and XML is one of the most up-front?

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.