Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8216475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:08:18+00:00 2026-06-07T12:08:18+00:00

I need to write an XML fragment to be consumed by a web service.

  • 0

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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:08:19+00:00Added an answer on June 7, 2026 at 12:08 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write an XML Schema for this XML language: <wfInfo> <worflow name=wf1>
I need to write a XML-based markup language. I'd like to provide an open
I need to write an XML schema that accept XMLs such as: <?xml version=1.0
First of all: What I need to achieve is write an Xml document Header,
I'm trying to write a Linq2XML query to query the following XML. I need
i need to write a schema that all xml instances are valid to it.
I need to write string to an xml file in Silverlight. When I used
I need to write model data ( CharField s only) to an XML file
I am using the .NET 4.0 Framework. I need to write some XML to
I need to write XML data to an encrypted file. I can read/write encrypted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.