1

我正在使用以下标头将对象序列化为 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
{
...
}
4

1 回答 1

1

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

于 2013-03-26T13:46:21.757 回答