![](/img/trans.png)
[英]How to remove xmlns attribute of a node other than root in an XDocument?
[英]Restsharp - Error attempting to serialize xmlns attribute on root node
當嘗試使用在根節點上包含xmlns屬性的RestSharp將對象序列化為XML時,我收到以下異常:
不能在同一開始元素標記中將前綴“”從“”重新定義為“ https://someurl.com ”。
我嘗試序列化的對象僅包含一個屬性(目前),即XmlNamespace:
[SerializeAs(Name = "root")]
public class Root
{
[SerializeAs(Name = "xmlns", Attribute = true)]
public String XmlNamespace { get; set; }
}
嘗試將我的對象添加到請求主體時會發生異常,如下所示:
Root requestBody = new Root();
requestBody.XmlNamespace = "https://someurl.com";
var request = new RestRequest();
request.Method = Method.POST;
request.Resource = "orders";
request.RequestFormat = DataFormat.Xml;
request.AddBody(requestBody); // exception occurs here
我嘗試使用RestRequest的XmlNamespace屬性以及為RestRequest實例化一個新的XmlSerializer,但是這些都沒有按照我嘗試訪問的API的要求將名稱空間附加到根節點。 有人碰巧知道如何在RestSharp中正確序列化xmlns屬性嗎?
我終於找到答案了。 看起來我被請求級別和XmlSerializer級別的XmlNamespace屬性的存在嚇到了。 對於遇到此問題的其他任何人,解決方法是在調用AddBody方法時定義命名空間,如下所示:
request.AddBody(requestBody, "https://someurl.com");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.