How can I change the name
of a tag
of an XML already generated?
I have an XML file of this style:
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:siiLR="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroLR.xsd" xmlns:sii="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroInformacion.xsd">
<soapenv:Header/>
<soapenv:Body>
<SuministroLRFacturasEmitidas xmlns:Sii="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroInformacion.xsd" xmlns:SiiLR="https://www2.agenciatributaria.gob.es/static_files/common/internet/dep/aplicaciones/es/aeat/ssii/fact/ws/SuministroLR.xsd">
<sii:Cabecera>
<sii:IDVersionSii>0.6</sii:IDVersionSii>
<sii:Titular>
<sii:NombreRazon>EMPRESAXXXX</sii:NombreRazon>
<sii:NIF>A84532501</sii:NIF>
</sii:Titular>
<sii:TipoComunicacion>A0</sii:TipoComunicacion>
</sii:Cabecera>
<siiLR:RegistroLRFacturasEmitidas>
<sii:PeriodoImpositivo>
<sii:Ejercicio>2015</sii:Ejercicio>
<sii:Periodo>01</sii:Periodo>
</sii:PeriodoImpositivo>
<siiLR:IDFactura>
<sii:IDEmisorFactura>
<sii:NIF>A84532501</sii:NIF>
</sii:IDEmisorFactura>
<sii:NumSerieFacturaEmisor>01</sii:NumSerieFacturaEmisor>
<sii:FechaExpedicionFacturaEmisor>15-01-2015</sii:FechaExpedicionFacturaEmisor>
</siiLR:IDFactura>
<siiLR:FacturaExpedida>
<sii:TipoFactura>F1</sii:TipoFactura>
<sii:ClaveRegimenEspecialOTrascendencia>01</sii:ClaveRegimenEspecialOTrascendencia>
<sii:ImporteTotal>26.70</sii:ImporteTotal>
<sii:DescripcionOperacion>CompraXXXXXXX</sii:DescripcionOperacion>
<sii:Contraparte>
<sii:NombreRazon>EMPRESAYYYYYYYY</sii:NombreRazon>
<sii:NIF>94234500B</sii:NIF>
</sii:Contraparte>
<sii:TipoDesglose>
<sii:DesgloseFactura>
<sii:Sujeta>
<sii:NoExenta>
<sii:TipoNoExenta>S1</sii:TipoNoExenta>
<sii:DesgloseIVA>
<sii:DetalleIVA>
<sii:TipoImpositivo>21</sii:TipoImpositivo>
<sii:BaseImponible>22.07</sii:BaseImponible>
<sii:CuotaRepercutida>4.63</sii:CuotaRepercutida>
<sii:TipoRecargoEquivalencia>0</sii:TipoRecargoEquivalencia>
<sii:CuotaRecargoEquivalencia>0</sii:CuotaRecargoEquivalencia>
</sii:DetalleIVA>
</sii:DesgloseIVA>
</sii:NoExenta>
</sii:Sujeta>
</sii:DesgloseFactura>
</sii:TipoDesglose>
</siiLR:FacturaExpedida>
</siiLR:RegistroLRFacturasEmitidas>
</SuministroLRFacturasEmitidas>
</soapenv:Body>
</soapenv:Envelope>
All tags that are within the level of the <soapenv:Body>
tag are prefixed with Sii
or SiiLR
.
The <SuministroLRFacturasEmitidas>
tag should have the prefix SiiLR
but it does not.
The <SuministroLRFacturasEmitidas>
tag must be <SiiLR:SuministroLRFacturasEmitidas>
Trying to change it is where I have the problem:
I tried:
Option 1:
Dim doc As New XmlDocument()
doc.Load(RutaDelXml)
Dim root As XmlNodeList = doc.GetElementsByTagName("SuministroLRFacturasEmitidas")
root(0).Name = "SiiLR:SuministroLRFacturasEmitidas"
But the Name is ReadOnly
so I can not change it.
Option 2:
Dim doc As New XmlDocument()
doc.Load(String.Format(RutaXml)
Dim Nodo As XmlNode = doc.SelectSingleNode("SuministroLRFacturasEmitidas")
If Nodo IsNot Nothing Then
Nodo.Prefix = "SiiLR"
End If
doc.Save(RutaXmlNueva)
By changing the Prefix
, in the Nodo
object, the Name shows me <SiiLR:SuministroLRFacturasEmitidas>
that is correct, but when making Save
of XmlDocument
, the change does not applies.
Option 3: With this I have achieved it, but I do not like it too much and I am sure there is a better way to do it:
Dim doc As New XmlDocument()
doc.Load(RutaXml)
Dim texto As String = doc.InnerXml
Dim encontrado As String = System.Text.RegularExpressions.Regex.Match(texto, "<SuministroLRFacturasEmitidas ").ToString
texto = System.Text.RegularExpressions.Regex.Replace(texto, "<SuministroLRFacturasEmitidas ", "<SiiLR:SuministroLRFacturasEmitidas ")
texto = System.Text.RegularExpressions.Regex.Replace(texto, "</SuministroLRFacturasEmitidas.*>", "</SiiLR:SuministroLRFacturasEmitidas>")
doc.InnerXml = texto
doc.Save(RutaXmlNueva)
I retrieve the content of the Xml
in a string
, I do the replace
through regular expressions and I save the result in a new xml
.
As I mention, option 3 works, but I would like your opinion about it.