6

I need to write an XML fragment to be consumed by a web service. Any xml declarations 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 the request criteria and then get the results.

The code is used like this:

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 a proper response, without the XML declaration or any namespaces. However, I would rather capture the data in a memory stream rather then a file.

Using the following method (taken from http://www.codeproject.com/Articles/58287/XML-Serialization-Tips-Tricks ) I am able to achieve results, but for some reason I have a ? listed as part of the 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());

} 

the resulting string is:

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

if I set OmitXmlDeclaration = false I get the following string:

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

Can anyone help me determine why the extra ? is there and how I can remove it?

Working SerializeToString method with no BOM

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());


}
Robert H
  • 11,520
  • 18
  • 68
  • 110
  • 2
    Funny it looks like part of the XML declaration is left in, either that or an encoding issue - have you tried any other encodings out of curiosity? – Charleh Jul 12 '12 at 16:46
  • that's what I thought as well, but I tried both UTF8 and unicode for the encoding with the same results. – Robert H Jul 12 '12 at 16:47
  • If you change OmitXmlDeclaration to false does it work (of course you get the XML declaration but does it output ok XML?) – Charleh Jul 12 '12 at 16:48
  • odd... string is returned as: ?1NAVS5001 – Robert H Jul 12 '12 at 16:49
  • So it looks like something to do with the stream then - have you checked the MemoryStream to see what the first byte is after writing?? At lease you can eliminate the encoding – Charleh Jul 12 '12 at 16:50
  • I haven't yet, although I was wondering if it could be the memoryStream causing the issue. – Robert H Jul 12 '12 at 16:52
  • Some info here: http://stackoverflow.com/questions/1317700/strip-byte-order-mark-from-string-in-c-sharp – Charleh Jul 12 '12 at 16:56

1 Answers1

4

You are seeing BOM (byte order mask) as first character in your string converted from stream's byte array.

Turn off outputting BOM and you'll be fine.

Use encoding object that does not generate BOM: UnicodeEncoding

settings.Encoding = new UnicodeEncoding(bigEndian:false,byteOrderMark:true)
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179