我来找你是因为我有问题。
总之,当我尝试将值添加到 Signature 元素的 xmlns 属性时,我收到以下错误。
西班牙语
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.
或英文
System.Xml.XmlException: 'The prefix' 'from' 'to' http://www.w3.org/2000/09/xmldsig# 'cannot be redefined within the same initial element tag.
我拥有的代码如下:
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();
它返回了以下 XML 结构:
<!--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>
问题是 xmlns 属性出现并且为空,我希望它具有以下值:
< Signature Id="SignOpenInvoicePeru" xmlns="http://www.w3.org/2000/09/xmldsig#" >
当我删除评论 // new XAttribute ("xmlns", " http://www.w3.org/2000/09/xmldsig# "
我收到以下错误:
西班牙语
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.
或英文
System.Xml.XmlException: 'The prefix' 'from' 'to' http://www.w3.org/2000/09/xmldsig# 'cannot be redefined within the same initial element tag.
我不知道为什么我会收到这个错误,但是请,我需要你的支持来给我一个想法。非常感谢您提前。