如何控制XmlSerializer

时间:2015-10-26 15:08:50

标签: c# namespaces xmlserializer

我正在尝试控制默认命名空间添加到XmlSerializer的输出的级别......

到目前为止,我已经......

<GetAccountDetailRequestStructure>
  <AccountRef xmlns="http://www.govtalk.gov.uk/NAC/GetAccountDetail">4026069</AccountRef>
  <AccountType xmlns="http://www.govtalk.gov.uk/NAC/GetAccountDetail">C</AccountType>
  <SelectionOptions xmlns="http://www.govtalk.gov.uk/NAC/GetAccountDetail">
    <FromDate>2000-01-01</FromDate>
    <ToDate>2015-10-23</ToDate>
    <IncludeAccountSummary>false</IncludeAccountSummary>
  </SelectionOptions>
</GetAccountDetailRequestStructure>

使用......

var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, "http://www.govtalk.gov.uk/NAC/GetAccountDetail");
var xs = new XmlSerializer(typeof(T));
xs.Serialize(xmlWriter, obj, ns);

但我想要的是......

<GetAccountDetailRequestStructure xmlns="http://www.govtalk.gov.uk/NAC/GetAccountDetail">
  <AccountRef>4026069</AccountRef>
  <AccountType>C</AccountType>
  <SelectionOptions>
    <FromDate>2000-01-01</FromDate>
    <ToDate>2015-10-23</ToDate>
    <IncludeAccountSummary>false</IncludeAccountSummary>
  </SelectionOptions>
</GetAccountDetailRequestStructure>

我认为它等同于第一个XML示例

1 个答案:

答案 0 :(得分:1)

尝试将默认命名空间传递给fputc(0x0A, file); 的{​​{3}}:

XmlSerializer