如何使用XML序列化来抑制名称空间的输出?

jun*_*one 2 c# xml-serialization

我使用XmlSerializer序列化了一个对象,并在输出中接收了不需要的命名空间属性.如何防止它在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; }
}
Run Code Online (Sandbox Code Playgroud)

输出:

<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>
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 5

var ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(writer, decision, ns);
Run Code Online (Sandbox Code Playgroud)

  • 另外,`var ns = new XmlSerializerNamespaces(new [] {XmlQualifiedName.Empty});`如果one-liners激动你. (2认同)