XML I need to insert XML2 in XML1

2
XML1


<?xml version="1.0" encoding="ISO-8859-1"?>
<EnvioDTE xmlns="EnvioXXX" version="1.0">
<SetDTE ID="Set728384">
<Caratula version="1.0">     
<Fecha>2015-06-02</Fecha>
<Numero>12345</Numero>
<Envio>2016-12-03T01:09:25</Envio>
</Caratula>
<DTE version="1.0">
<Encabezado>
<IdDoc>
<TipoD>105</TipoD>
<Folio>111705</Folio>
<FchEmis>2016-12-03</FchEmis>
</IdDoc>
<Emisor>
<IDEmisor>7251827106-1</IDEmisor>
<RznSoc>Comercializadora SPA</RznSoc>
</Emisor>
<Receptor>
<IDRecep>1216298490-3</IDRecep>
<RznSocRecep>Juana Isabel </RznSocRecep>
</Receptor>
<Totales>
<Monto>861758</Monto>
</Totales>
</Encabezado>
</DTE>
               -------------------------------------Aqui necesito insertar el XML2
</SetDTE>
</EnvioDTE>



XML2

<?xml version="1.0" encoding="ISO-8859-1"?>
<DTE version="1.0">
<Encabezado>
<IdDoc>
<TipoD>110</TipoD>
<Folio>111705</Folio>
<FchEmis>2016-10-01</FchEmis>
</IdDoc>
<Emisor>
<IDEmisor>12345827106-1</IDEmisor>
<RznSoc>El Librero</RznSoc>
</Emisor>
<Receptor>
<IDRecep>23678998490-3</IDRecep>
<RznSocRecep>Pedro Perez </RznSocRecep>
</Receptor>
<Totales>
<Monto>334589</Monto>
</Totales>
</Encabezado>
</DTE>

and I'm trying to do it like that and it does not work for me

 Private Sub AgregaD()
 Dim doc1 As XElement = XElement.Load("XML1.xml")
 Dim NodoP As XElement = (From nodo In doc1.Descendants("SetDTE") Where nodo.Attribute("ID").Value = "Set728384").FirstOrDefault()

 Dim doc2 As XElement = XElement.Load("XML2.xml")

 Dim nuevoNodo As New XElement(doc2)
 NodoP.Add(nuevoNodo)
 NodoP.Save("ResultadoD.xml")
 End Sub
    
asked by QBox 19.12.2016 в 20:43
source

1 answer

0

Since the first document has the declaration <EnvioDTE xmlns="EnvioXXX" version="1.0"> in your VB.NET program you need to work with Dim df as XNamespace = doc1.Root.Name.Namespace and then use it in Dim NodoP As XElement = (From nodo In doc1.Descendants(df + "SetDTE") Where nodo.Attribute("ID").Value = "Set728384").FirstOrDefault() . That way you can select the item and add a new one as you have dealt with NodoP.Add(nuevoNodo) .

However, if the elements of the second document do not use the same namespace xmlns="EnvioXXX" the result may not be what you want, tell us if you need more help.

    
answered by 31.12.2016 в 17:42