2

I'm trying to create a Peppol Invoice xml by using C# XmlSerializer. This all works fine except for the namespaces.

The Peppol standard format has namespaces in the XmlRoot, but also in one of the XmlElements.

Xml example

<?xml version="1.0" encoding="UTF-8"?>
<StandardBusinessDocument xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
  <StandardBusinessDocumentHeader>

    ...

  </StandardBusinessDocumentHeader>
  <Invoice 
    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
    xmlns:ccts="urn:un:unece:uncefact:documentation:2" 
    xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
    xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

      ...
   </Invoice>
</StandardBusinessDocument>

If I add all namespaces with XmlSerializerNamespaces, all these namespaces are assigned to 'StandardBusinessDocument '

Code example:

        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("cac", "aaa");
        ns.Add("cbc", "bbb");
        ns.Add("ccts", "ccc");

        XmlSerializer serializer = new XmlSerializer(data.GetType());
        using (StringWriter writer = new StringWriter())
        {
            serializer.Serialize(writer, data, ns);
            return writer.ToString();
        }

Is there any way to add these namespaces to the Invoice XmlElement?

Thanks in advance

J. Cuppens
  • 41
  • 3
  • Declaring those namespaces in a higher enclosing element shouldn't change the *information content* of the XML. If whatever is receiving this XML is properly written (i.e. uses XML tooling rather than manual string manipulations), it should accept it just the same. – Damien_The_Unbeliever Sep 12 '18 at 09:52
  • The surrounding `StandardBusinessDocument` (SBD) should be parsed separately. The `Invoice` is an `xs:any` inside the SBD that should be treated independently. – Philip Helger Sep 27 '18 at 16:25
  • It appears "duplicated" but is not. target is do so something like sample1 I got that problem in one of my projects and i did found a solution What you need to do is in the "new_root" class add namespace declaration like this. [XmlNamespaceDeclarations] public XmlSerializerNamespaces newns;) and in your item1 and item2 classes decore to use your namespace like this [XmlElement("item1", Namespace = "url:new:ns")] – Guillermo Gutiérrez Feb 06 '20 at 17:21

0 Answers0