简体   繁体   English

如何为DataContract设置命名空间?

[英]How to set Namespace for DataContract?

I have a situation where I have a REST controller that is called by a user and this controller then requests data from upstream and receives JSON as a response. 我遇到一种情况,我有一个由用户调用的REST控制器,然后该控制器从上游请求数据并接收JSON作为响应。 This JSON is then transformed into XML and send back to the user as a response. 然后,此JSON转换为XML,并作为响应发送回用户。 The problem is that I am not able to set specific namespace for the XML root element. 问题是我无法为XML根元素设置特定的名称空间。 I am using DataContractSerializer. 我正在使用DataContractSerializer。

I am not really experienced with .NET and have previously worked mainly with JSON so i am a bit lost as to what to try next. 我对.NET并没有真正的经验,并且以前主要使用JSON,所以我对下一步的尝试感到迷茫。

I have tried to set the namespace using ContractNamespaceAttribute like: 我试图使用ContractNamespaceAttribute设置名称空间,例如:

[assembly: ContractNamespaceAttribute("http://schemas.datacontract.org/2004/07/APIBridge.Models", ClrNamespace = "APIBridge.Models")]
namespace APIBridge.Models
{
  [DataContract]
  public class Order
  {
    // DataMembers here...
  }
}

And I also tried setting the namespace in the DataContracAttribute like: 我还尝试在DataContracAttribute中设置名称空间,例如:

namespace APIBridge.Models
{
  [DataContract(Name = "Order", Namespace = 
  "http://schemas.datacontract.org/2004/07/APIBridge.Models")]
  public class Order
  {
    // Datamembers here...
  }
}

How I would like the namespace to be set is: 我希望如何设置名称空间是:

<ArrayOfOrder xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/APIBridge.Models">

But the actual result is: 但是实际结果是:

<ArrayOfOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?redirectedfrom=MSDN&view=netframework-4.8 https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.serialization.datacontractattribute?redirectedfrom=MSDN&view=netframework-4.8

In the DataContractAttribute documentation above it says: 在上面的DataContractAttribute文档中,它说:

"By default, when you apply the DataContractAttribute to a class, it uses the class name as the local name and the class's namespace (prefixed with " http://schemas.datacontract.org/2004/07/ ") as the namespace URI." “默认情况下,将DataContractAttribute应用于类时,它将使用类名称作为本地名称,并使用该类的名称空间(前缀为“ http://schemas.datacontract.org/2004/07/ ”)作为名称空间URI ”。

This actually would be the desired result but also as a default namespace I get the same result that is mentioned above. 实际上,这将是期望的结果,但作为默认的命名空间,我将得到与上述相同的结果。 Why is this? 为什么是这样?

Any ideas? 有任何想法吗?

UPDATE: Below requested service operation 更新:低于要求的服务操作

        public List<Order> loadOrdersBasic(UserOptions userOpts)
        {
            List<Order> orders = new List<Order>();
            HttpClient httpClient = AuthenticationHelper.CreateHttpClient(userOpts, options);
            String url = String.Format("api/orders?supplier_no={0}", userOpts.SupplierId);
            HttpResponseMessage response = httpClient.GetAsync(url).Result;
            if (response.IsSuccessStatusCode)
            {
                orders = response.Content.ReadAsAsync<List<Order>>().Result;
            }
            else {
                throw new ServiceException(getHttpErrorMessage(url, response));
            }
            return orders;
        }

Answering to my own question. 回答我自己的问题。

Turned out that I had missed one line in my configuration file where XmlMediaTypeFormatter was set to use XmlSerializer instead of DataContractSerializer. 原来,我在配置文件中错过了XmlMediaTypeFormatter设置为使用XmlSerializer而不是DataContractSerializer的一行。 This overrode the default namespace. 这将覆盖默认名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM