2

I have serialized an object using XmlSerializer and am receiving unwanted namespace attributes in my output. How can I prevent it from printing these namespaces in the XML?

// write and close the bar
XmlSerializer serializer = new XmlSerializer(typeof( DecisionBar));

serializer.Serialize(writer, decision);

public class DecisionBar
{
    public DateTime bartime { get; set; }
    public string frequency { get; set; }
    public bool HH7 { get; set; }
    public bool crossover { get; set; }
    public double mfe { get; set; }
    public double mae { get; set; }
    public double entryPointLong { get; set; }
    public double entryPointShort { get; set; }
}

Output:

<DecisionBar xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <bartime>2012-07-23T07:22:00</bartime>
  <frequency>1 MINUTES</frequency>
  <HH7>true</HH7>
  <crossover>false</crossover>
  <mfe>0</mfe>
  <mae>0</mae>
  <entryPointLong>1.2139</entryPointLong>
  <entryPointShort>1.212</entryPointShort>
</DecisionBar>

1 Answer 1

5
var ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(writer, decision, ns);
Sign up to request clarification or add additional context in comments.

4 Comments

In addition, var ns = new XmlSerializerNamespaces(new [] { XmlQualifiedName.Empty }); if one-liners excite you.
@marc that's quite cute ; I can't say that I'd noticed XmlQualifiedName.Empty before.
how do i get rid of <?xml version="1.0" encoding="utf-8"?> that comes there?
@junkone XmlWriterSettings, OmitXmlDeclaration - use it when creating the XmlWriter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.