c# - XML 空白命名空间

标签 c# xml serialization namespaces

我正在使用以下 header 将对象序列化为 XML。

<agr:ABWInvoice 
    xmlns:agr="http://services.agresso.com/schema/ABWInvoice/2011/11/14"
    xsi:schemaLocation="http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" 
    xmlns:agrlib="http://services.agresso.com/schema/ABWSchemaLib/2011/11/14" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 >

但是,我想要像下面这样的东西:(唯一的区别是在没有命名空间的第一个 xmlns 中)
<agr:ABWInvoice 
   xmlns="http://services.agresso.com/schema/ABWInvoice/2011/11/14"
   xsi:schemaLocation="http://services.agresso.com/schema/ABWInvoice/2011/11/14 http://services.agresso.com/schema/ABWInvoice/2011/11/14/ABWInvoice.xsd" 
   xmlns:agrlib="http://services.agresso.com/schema/ABWSchemaLib/2011/11/14" 
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  >

我使用以下代码:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("agrlib", "http://services.agresso.com/schema/ABWSchemaLib/2006/11/20");
ns.Add("agr", "http://services.agresso.com/schema/ABWInvoice/2006/11/20");

XmlSerializer serializer = new XmlSerializer(typeof(ABWInvoice2006));
TextWriter textWriter = new StreamWriter(xmlFile);
serializer.Serialize(textWriter, abwInvoice, ns);
textWriter.Close();

我也尝试过以下操作,但没有给出所需的输出:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
ns.Add("agrlib", "http://services.agresso.com/schema/ABWSchemaLib/2006/11/20");
ns.Add("", "http://services.agresso.com/schema/ABWInvoice/2006/11/20");

更新:@Vladimir Frolov 引导我使用以下方法解决问题:
[Serializable]
[XmlRootAttribute(Namespace = "http://services.agresso.com/schema/ABWInvoice/2006/11/20", IsNullable = true)]
public class ABWInvoice2006
{
...
}

最佳答案

尝试在 XmlSerializer 构造函数中指定 XmlRootAttribute。
这是一个 example .

关于c# - XML 空白命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15638462/

相关文章:

xml - XML 文件的替代文件扩展名?

java - 适用于 Marshaller setSchema 的 XML 模式

c# - 如何在 Silverlight 中序列化派生类

C#:需要从文件名路径中删除最后一个文件夹

c# - 使用 WebClient 调用服务时获取 JSON 而不是 XML 文件

java.lang.ClassCastException : GridLayout. LayoutParams 无法转换为 Frame.LayoutParams

python - 任何类型的棉花糖领域

serialization - Akka集群自定义序列化器:

c# - 您可以在 C# 中循环访问 List<> 中的项目吗

c# - Silverlight 有什么可能?