简体   繁体   中英

xmlns attribute cannot be added as an attribute to XML element

I come to you because I have a problem.

In summary, when I try to add the value to the xmlns attribute of the Signature element, I get the following error.

Spanish

System.Xml.XmlException : 'No se puede volver a definir el prefijo '' de '' a 'http://www.w3.org/2000/09/xmldsig#' dentro de la misma etiqueta de elemento inicial.

or English

System.Xml.XmlException: 'The prefix' 'from' 'to' http://www.w3.org/2000/09/xmldsig# 'cannot be redefined within the same initial element tag.

The code that I have is the following:

XNamespace _Xmlns = "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2";
XNamespace _Cac = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
XNamespace _Cbc = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";
XNamespace _Ds = "http://www.w3.org/2000/09/xmldsig#";
XNamespace _Ext = "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2";
XNamespace _Xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace _Udt = "urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2";
XNamespace _Qdt = "urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2";
XDocument arbolXML = new XDocument(
new XDeclaration("1.0", "utf-8", "no"),
new XComment("este es un comentario"),
new XElement(
           _Xmlns + "Invoice",
            new XAttribute(XNamespace.Xmlns + "cac", _Cac.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "cbc", _Cbc.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "ds", _Ds.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "ext", _Ext.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "xsi", _Xsi.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "udt", _Udt.NamespaceName),
            new XAttribute(XNamespace.Xmlns + "qdt", _Qdt.NamespaceName),
                    new XElement(_Ext + "UBLExtensions"
                    , new XElement(_Ext + "UBLExtension",
                            new XElement(_Ext + "ExtensionContent",
                                new XElement("Signature", new XAttribute("Id", "SignOpenInvoicePeru") //, new XAttribute("xmlns", "http://www.w3.org/2000/09/xmldsig#")
                                )
                            )
                      )
                    )
           )
);


string cadena = arbolXML.ToString();

Which returns me the following XML structure:

<!--este es un comentario-->
<Invoice  xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
    xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
    xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
    xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
    <ext:UBLExtensions>
        <ext:UBLExtension>
            <ext:ExtensionContent>
                <Signature Id="SignOpenInvoicePeru" xmlns="" />
            </ext:ExtensionContent>
            </ext:UBLExtension>
        </ext:UBLExtensions>
    </Invoice>

The problem is that the xmlns attribute appears and is empty, and I want it to have the following value :

< Signature Id="SignOpenInvoicePeru" xmlns="http://www.w3.org/2000/09/xmldsig#" >

when I remove the comment // new XAttribute ("xmlns", " http://www.w3.org/2000/09/xmldsig# "

I get the following error:

Spanish

System.Xml.XmlException : 'No se puede volver a definir el prefijo '' de '' a 'http://www.w3.org/2000/09/xmldsig#' dentro de la misma etiqueta de elemento inicial.

or English

System.Xml.XmlException: 'The prefix' 'from' 'to' http://www.w3.org/2000/09/xmldsig# 'cannot be redefined within the same initial element tag.

I don't know why I get that error, but please, I need your support in giving me an idea. Thank you very much in advance.

If the Signature Element should follow the namespace http://www.w3.org/2000/09/xmldsig# then it should be prefixed with ds: as this is the namespace you define in the root document.

   <ds:Signature Id="SignOpenInvoicePeru" />

to achieve that use

   new XElement(_Ds + "Signature", new XAttribute("Id", "SignOpenInvoicePeru")

maybe I am missing something, but I believe it is not right to specify the xmlns at the nested element level, especially as this namespace is already declared in the root element.

   <Invoice xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM