mrd
mrd

Reputation: 2183

XML blank namespace

I am serializing an object to XML with following header.

<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"
 >

But, I want something like below: (The only difference is in first xmlns without namespace)

<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"
  >

I use following code:

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();

I have tried following also, but does not give desired output:

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");

Update:

@Vladimir Frolov lead me to solve the problem using following:

[Serializable]
[XmlRootAttribute(Namespace = "http://services.agresso.com/schema/ABWInvoice/2006/11/20", IsNullable = true)]
public class ABWInvoice2006
{
...
}

Upvotes: 1

Views: 249

Answers (1)

Vladimir
Vladimir

Reputation: 7475

Try to specify XmlRootAttribute in XmlSerializer constructor. Here is an example.

Upvotes: 1

Related Questions