2012-07-23 38 views
2

我已經使用XmlSerializer序列化對象,並在我的輸出中接收不需要的名稱空間屬性。我如何防止它在XML中打印這些名稱空間?如何用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; } 
} 

輸出:

<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> 

回答

5
var ns = new XmlSerializerNamespaces(); 
ns.Add("", ""); 
serializer.Serialize(writer, decision, ns); 
+2

此外,'VAR NS =新XmlSerializerNamespaces(新[] {XmlQualifiedName.Empty});'如果單行激發你。 – Marc 2012-07-23 14:37:22

+0

@marc很可愛;我不能說我之前注意到了XmlQualifiedName.Empty。 – 2012-07-23 14:39:37

+0

我該如何擺脫<?xml version =「1.0」encoding =「utf-8」?>那裏呢? – junkone 2012-07-23 16:52:47

相關問題