0

Happy new year everyobdy, After I serialize object using StringWriter and return output response and send to client. But when client receive the response: It has a tag like this:

<string xmlns="http://schemas.microsoft.com/...">
<?xml version="1.0" encoding="utf-16">
   <trx>.....</trx>
</string>

So, my questions are: 1. How should I return the correct format xml like below:

 <?xml version="1.0" encoding="utf-16">
       <trx>.....</trx>

2. If I send the response as byte[] using MemoryStream, does the client receive correct with encoding="utf-8" ?

Thank you so much! This is the code path I used for serializing:

StringWriter textWriter = new StringWriter();
XmlWriter tw = null;
XmlWriterSettings settings new XmlWriterSettings{
   Encoding =UnicodeEncoding.UTF8,
   Indent =false,
   OmitXmlDeclaration =false
};
var serialize1 =new XmlSerializer(typeof(OTrx));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("","");
tw=XmlWriter.Create(textWriter,settings);
serializer1.Serialize(tw,oTrx,ns);
var output=textWriter.ToString();
return output
HienND
  • 35
  • 1
  • 7
  • Can you provide a code snippet showing how you're doing your serialization. You simply state 'using StringWriter'. Also, regarding #2, it depends on how you're creating your response, so would need a code snippet of that as well to help. – mhand Jan 04 '19 at 11:22

2 Answers2

0

Not sure if you are using .net core. If yes, then you can use this middleware nuget package - Microsoft.AspNetCore.Mvc.Formatters.Xml

Please refer this URL for code examples and detailed explanation.

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37
0

It would be useful if you shared some sample code but have you considered this?

If you return a string, you'll get a string.

Txugo
  • 5,008
  • 5
  • 33
  • 40
  • Yes, I want to return the output like below, so what should I return the type of output: ..... – HienND Jan 05 '19 at 02:45