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 に答える