簡體   English   中英

如何控制XmlSerializer在何處添加默認名稱空間

[英]How to control where the default namespace is added by the 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示例

嘗試將默認名稱空間也傳遞給XmlSerializer構造函數

const string defaultNamespace = "http://www.govtalk.gov.uk/NAC/GetAccountDetail";
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, defaultNamespace);

// Note the 2nd constructor argument.
var xs = new XmlSerializer(typeof(T), defaultNamespace);

xs.Serialize(xmlWriter, obj, ns);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM