我希望我的输出看起来像这样
<OrderContainer xmlns="http://blabla/api/products" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
所以我将以下内容添加到我的 XmlSerializer
XmlSerializer x = new XmlSerializer(typeof(OrderContainer));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "http://blabla/api/products");
ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
// do stuff..
x.Serialize(stream, orderContainer, ns);
但是现在我明白了
<OrderContainer xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
那么如何编辑默认命名空间呢?
我的对象定义是这样的:
[System.Runtime.Serialization.DataContractAttribute(Name="OrderContainer", Namespace="http://blabla/api/products")]
[System.SerializableAttribute()]
public partial class OrderContainer
最佳答案
您可以使用 XmlSerializer constructor除了要序列化的类型之外,它还采用默认 namespace :
var x = new XmlSerializer(
typeof(OrderContainer),
"http://blabla/api/products");
var ns = new XmlSerializerNamespaces();
ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
x.Serialize(stream, orderContainer, ns);
关于c# - 使用 XmlSerializer 添加不带前缀的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1861049/