Writing an XML fragment using XmlWriterSettings and XmlSerializer gives an extra character

I need to write an XML fragment that will be used by the web service. Any xml ads cause the web service to reject the request. To support this, I have the following class:

public class ContentQueryCriteria { public int Type { get; set; } public string Value { get; set; } public int Condition { get; set; } } 

which allow me to set query criteria and then get the results.

The code is used as follows:

 ContentQueryCriteria content = new ContentQueryCriteria(); content.Type = 1; content.Value = "NAVS500"; content.Condition = 1; string requestBody = SerializeToString(content); Console.WriteLine(requestBody); 

When I serialize this to an XML file, I get the correct answer without declaring XML or any namespaces. However, I would rather capture the data in a memory stream rather than in a file.

Using the following method (taken from http://www.codeproject.com/Articles/58287/XML-Serialization-Tips-Tricks ), I can achieve results, but for some reason do I have? listed as part of a string.

 public static string SerializeToString(object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); MemoryStream ms = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Encoding = Encoding.Unicode; XmlWriter writer = XmlWriter.Create(ms, settings); serializer.Serialize(writer, obj, ns); return Encoding.Unicode.GetString(ms.ToArray()); } 

resulting string:

 ?<ContentQueryCriteria><Type>1</Type><Value>NAVS500</Value><Condition>1</Condition></ContentQueryCriteria> 

if I set OmitXmlDeclaration = false, I get the following line:

 ?<?xml version="1.0" encoding="utf-16"?><ContentQueryCriteria><Type>1</Type><Value>NAVS500</Value><Condition>1</Condition></ContentQueryCriteria> 

Can someone help me determine why extra? is and how can I delete it?

SerializeToString working method without specification

 public static string SerializeToString(object obj) { XmlSerializer serializer = new XmlSerializer(obj.GetType()); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); MemoryStream ms = new MemoryStream(); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.Encoding = new UnicodeEncoding(bigEndian: false, byteOrderMark: false); XmlWriter writer = XmlWriter.Create(ms, settings); serializer.Serialize(writer, obj, ns); return Encoding.Unicode.GetString(ms.ToArray()); } 
+6
source share
1 answer

You see the BOM (byte byte mask) as the first character in your string converted from a stream byte array.

Turn off the specification output and everything will be fine.

Use an encoding object that does not generate a specification: UnicodeEncoding

 settings.Encoding = new UnicodeEncoding(bigEndian:false,byteOrderMark:true) 
+3
source

Source: https://habr.com/ru/post/920347/


All Articles