How to add a prefix to XML document on every different node


Ask A Question





Login To post a question

I have the following XML generated by my code:

<?xml version="1.0" encoding="utf-16"?>
<Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.dian.gov.co/contratos/facturaelectronica/v1">
  <UBLExtensions xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
    <UBLExtension />
  </UBLExtensions>
  <UBLVersionID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">UBL 2.0</UBLVersionID>


  <AccountingCustomerParty>
    <AdditionalAccountID xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">2</AdditionalAccountID>
    <Party>
      <PartyIdentification xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
        <ID schemeID="22" schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)" xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">8355990</ID>
      </PartyIdentification>
      <PhysicalLocation>
        <Address>
          <Department xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Tolima</Department>
          <CitySubdivisionName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Centro</CitySubdivisionName>
          <CityName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Guamo</CityName>
          <AddressLine xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
            <Line xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">carrera 8 Nº 6C - 39</Line>
          </AddressLine>
          <Country xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
            <IdentificationCode xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">CO</IdentificationCode>
          </Country>
        </Address>
      </PhysicalLocation>
      <PartyTaxScheme>
        <TaxLevelCode xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">0</TaxLevelCode>
      </PartyTaxScheme>
      <Person>
        <FirstName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Primer-N</FirstName>
        <FamilyName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Apellido-8355990</FamilyName>
        <MiddleName xmlns="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2">Segundo-N</MiddleName>
      </Person>
    </Party>
  </AccountingCustomerParty>

However I need to generate it with the following PREFIXes everywhere, No need to read everything but please note that each NODE has a prefix.

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<fe:Invoice xmlns:fe="http://www.dian.gov.co/contratos/facturaelectronica/v1"

  <ext:UBLExtensions>
    <ext:UBLExtension>
      <ext:ExtensionContent>
        <sts:DianExtensions>
          <sts:InvoiceControl>
            <sts:InvoiceAuthorization>545939</sts:InvoiceAuthorization>
            <sts:AuthorizationPeriod>
              <cbc:StartDate>2013-09-06</cbc:StartDate>
              <cbc:EndDate>2015-09-06</cbc:EndDate>
            </sts:AuthorizationPeriod>
            <sts:AuthorizedInvoices>
              <sts:Prefix>41</sts:Prefix>
              <sts:From>70263356</sts:From>
              <sts:To>79999999</sts:To>
            </sts:AuthorizedInvoices>
          </sts:InvoiceControl>

          <sts:SoftwareProvider>
            <sts:ProviderID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">700085371</sts:ProviderID>
            <sts:SoftwareID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">248c088c-abae-49d5-864f-00f3bba701e3</sts:SoftwareID>
          </sts:SoftwareProvider>
          <sts:SoftwareSecurityCode schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">
            5e48e26456aa71cd1be102ea4a8de3b7038f409f37c21dc34f6a5e441885f6a80369af74016b909f9a4ac73ada2d2d93
          </sts:SoftwareSecurityCode>
        </sts:DianExtensions>
      </ext:ExtensionContent>
    </ext:UBLExtension>
    <ext:UBLExtension>

    </ext:UBLExtension>
  </ext:UBLExtensions>
  <cbc:UBLVersionID>UBL 2.0</cbc:UBLVersionID>
  <cbc:ProfileID>DIAN 1.0</cbc:ProfileID>
  <cbc:ID>4171263356</cbc:ID>
  <cbc:UUID schemeAgencyID="195" schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)">c1db672511b492915a6f88a2aeed9f2f7eae0f38</cbc:UUID>
  <cbc:IssueDate>2015-06-26</cbc:IssueDate>
  <cbc:IssueTime>22:19:12</cbc:IssueTime>
  <cbc:InvoiceTypeCode listAgencyID="195" listAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)" listSchemeURI="http://www.dian.gov.co/contratos/facturaelectronica/v1/InvoiceType">1</cbc:InvoiceTypeCode>
  <cbc:Note>
    Set de pruebas = f-s0001_700085371_248c0_R545939-41-9715_0A_700085371 2015-06-30 sde rango #9709
    hasta 9822; numeracion del 9716 NumFac: 4171263356 FecFac: 20150626221912 ValFac: 10121904.00 CodImp1: 01
    ValImp1: 1619504.64 CodImp2: 02 ValImp2: 0.00 CodImp3: 03 ValImp3: 419046.82 ValImp: 12160455.46 NitOFE:
    700085371 TipAdq: 22 NumAdq: 8355990 String:
    41712633562015062622191210121904.00011619504.64020.0003419046.8212160455.46700085371228355990693ff
    6f2a553c3646a063436fd4dd9ded0311471
  </cbc:Note>
  <cbc:DocumentCurrencyCode>COP</cbc:DocumentCurrencyCode>



  </fe:InvoiceLine>
</fe:Invoice>

The code is as follows:

[HttpGet]
        [ActionName("GenerarFacturaXML")]
        [Route("GenerarFacturaXML")]
        public string generarFacturaPrueba()
        {
            InvoiceType factura = new InvoiceType();

            InvoiceControl control = new InvoiceControl();
            control.InvoiceAuthorization = 545939;
            PeriodType fechaAutorización = new PeriodType { StartDate = new StartDateType { Value = Convert.ToDateTime("2013-09-06") }, EndTime = new EndTimeType { Value = Convert.ToDateTime("2015-09-06") } };
            control.AuthorizationPeriod = fechaAutorización;
            AuthrorizedInvoices fAutorizadas = new AuthrorizedInvoices();
            fAutorizadas.Prefix = new TextType { Value = "41" };
            fAutorizadas.From = 70263356;
            fAutorizadas.To = 79999999;

            control.AuthorizedInvoices = fAutorizadas;

            UBLExtensionType extensionUBL = new UBLExtensionType();

            DianExtensionsType extensionDIAN = new DianExtensionsType();

            extensionDIAN.InvoiceControl = control;
            CountryType fuenteRecibo = new CountryType();

            IdentificationCodeType ict = new IdentificationCodeType();
            ict.listAgencyID = "6";
            ict.listAgencyName = "United Nations Economic Commission for Europe";
            ict.listSchemeURI = "urn:oasis:names:specification:ubl:codelist:gc:CountryIdentificationCode-2.0";
            ict.Value = "CO";

            fuenteRecibo.IdentificationCode = ict;

            extensionDIAN.InvoiceSource = fuenteRecibo;

            SoftwareProvider sp = new SoftwareProvider();
            sp.ProviderID = new IdentifierType
            {
                schemeAgencyID = "195",
                schemeAgencyName = "CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",
                Value = "700085371"
            };
            sp.SoftwareID = new IdentifierType
            {
                schemeAgencyID = "195",
                schemeAgencyName = "CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",
                Value = "248c088c-abae-49d5-864f-00f3bba701e3"
            };

            extensionDIAN.SoftwareProvider = sp;

            extensionDIAN.SoftwareSecurityCode = new IdentifierType
            {
                schemeAgencyID = "195",
                schemeAgencyName = "CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",
                Value = "5e48e26456aa71cd1be102ea4a8de3b7038f409f37c21dc34f6a5e441885f6a80369af74016b909f9a4ac73ada2d2d93"
            };

            XmlSerializer xsSubmit = new XmlSerializer(typeof(DianExtensionsType));
            var xmlExtensionDian = "";
            XmlDocument docFactura = new XmlDocument();
            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    xsSubmit.Serialize(writer, extensionDIAN);
                    xmlExtensionDian = sww.ToString(); // Your XML
                }

                docFactura.LoadXml(xmlExtensionDian);
            }
            UBLExtensionType ex = new UBLExtensionType();

            ex.ExtensionContent = docFactura.CreateElement("Invoice");
            ex.ExtensionContent.InnerXml = xmlExtensionDian;
            UBLExtensionsType ublextensions = new UBLExtensionsType();
            ublextensions.UBLExtension = new UBLExtensionType[2];
            factura.UBLExtensions = ublextensions.UBLExtension;
            factura.UBLExtensions[0] = new UBLExtensionType();
            //factura.UBLExtensions[0] = ex;//.ExtensionContent.InnerXml = xmlExtensionDian;

            SignatureType st = new SignatureType();
            st.CanonicalizationMethod = new CanonicalizationMethodType
            {
                Value = "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
            };
            st.SignatureMethod = new SignatureMethodType
            {
                Value = "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
            };
            st.OriginalDocumentReference = new DocumentReferenceType
            {
                ID = new IDType { Value = "xmldsig-79c270e3-50bb-4fcf-b9bc-3a95bcf2466d-ref0" }
            };//Parece que no es por acá

            //factura.UBLExtensions[1].ExtensionContent.Value = "";//string en formato xml de la firma electrónica requerida por la DIAN

            factura.UBLVersionID = new UBLVersionIDType
            {
                Value = "UBL 2.0"
            };
            factura.ProfileID = new ProfileIDType
            {
                Value = "DIAN 1.0"
            };
            factura.ID = new IDType
            {
                Value = "4171263356"
            };

            factura.UUID = new UUIDType
            {
                schemeAgencyID = "195",
                schemeAgencyName = "CO, DIAN(Direccion de Impuestos y Aduanas Nacionales)",
                Value = "c1db672511b492915a6f88a2aeed9f2f7eae0f38"
            };

            factura.IssueDate = new IssueDateType
            {
                Value = Convert.ToDateTime("2015-06-26")
            };
            factura.IssueTime = new IssueTimeType
            {
                Value = Convert.ToDateTime("2015-06-26 22:19:12")
            };

            factura.InvoiceTypeCode = new InvoiceTypeCodeType
            {
                listAgencyID = "195",
                listAgencyName = "CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",
                listSchemeURI = "http://www.dian.gov.co/contratos/facturaelectronica/v1/InvoiceType" ,
                Value ="1" 
            };

            factura.Note = new NoteType[]
            {
                new NoteType {Value= "Set de pruebas = f-s0001_700085371_248c0_R545939-41-9715_0A_700085371 2015-06-30 sde rango #9709"
                                    +" hasta 9822; numeracion del 9716 NumFac: 4171263356 FecFac: 20150626221912 ValFac: 10121904.00 CodImp1: 01"
                                    +" ValImp1: 1619504.64 CodImp2: 02 ValImp2: 0.00 CodImp3: 03 ValImp3: 419046.82 ValImp: 12160455.46 NitOFE:"
                                    +" 700085371 TipAdq: 22 NumAdq: 8355990 String:"
                                    +" 41712633562015062622191210121904.00011619504.64020.0003419046.8212160455.46700085371228355990693ff"
                                    +" 6f2a553c3646a063436fd4dd9ded0311471" }
            };

            factura.DocumentCurrencyCode = new DocumentCurrencyCodeType
            {                
                Value = "COP"
            };

            factura.AccountingSupplierParty = new SupplierPartyType
            {
                AdditionalAccountID = new AdditionalAccountIDType
                {
                    Value = "1"
                },
                Party = new PartyType
                {
                        PartyIdentification = new PartyIdentificationType[]
                        {
                            new PartyIdentificationType{ ID= new IDType{ Value ="700085371", schemeAgencyID="195",schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",schemeID="31"} }
                        },
                        PartyName= new PartyNameType[]
                        {
                            new PartyNameType
                            {
                                Name= new NameType1 { Value="PJ - 700085371 - Adquiriente FE"}
                            }
                        },
                        PhysicalLocation= new LocationType1
                        {
                            Address = new AddressType1
                            {
                                Department = new DepartmentType
                                {
                                    Value ="Bolivar"
                                },
                                CitySubdivisionName = new CitySubdivisionNameType
                                {
                                    Value= "Centro"
                                },
                                CityName = new CityNameType
                                {
                                    Value= "Pasa Caballos"
                                },
                                AddressLine = new AddressLineType[]
                                {
                                    new AddressLineType
                                    {
                                       Line=new LineType
                                       {
                                            Value = "carrera 8 Nº 6C - 60"
                                       }
                                    }
                                },
                                Country= new CountryType
                                {
                                    IdentificationCode= new IdentificationCodeType
                                    {
                                        Value="CO"                                        
                                    }
                                }
                            }
                        },
                        PartyTaxScheme = new PartyTaxSchemeType[]
                        {
                            new PartyTaxSchemeType
                            {
                                TaxLevelCode = new TaxLevelCodeType
                                {
                                    Value="0"
                                }
                            }
                        },
                        PartyLegalEntity = new PartyLegalEntityType[]
                        {
                            new PartyLegalEntityType
                            {

                                RegistrationName= new RegistrationNameType
                                {                                    
                                    Value="PJ - 700085371"
                                }
                            }
                        }
                }                
            };

            factura.AccountingCustomerParty = new CustomerPartyType
            {
                AdditionalAccountID = new AdditionalAccountIDType
                {
                    Value = "2"
                },
                Party = new PartyType
                {
                    PartyIdentification = new PartyIdentificationType[]
                    {
                        new PartyIdentificationType
                        {
                            ID=new IDType
                            {
                                schemeAgencyID="195",
                                schemeAgencyName="CO, DIAN (Direccion de Impuestos y Aduanas Nacionales)",
                                schemeID="22",
                                Value="8355990"
                            }
                        }
                    },
                    PhysicalLocation = new LocationType1
                    {
                        Address = new AddressType1
                        {
                            Department = new DepartmentType
                            {
                                Value = "Tolima"
                            },
                            CitySubdivisionName = new CitySubdivisionNameType
                            {
                                Value = "Centro"
                            },
                            CityName = new CityNameType
                            {
                                Value = "Guamo"
                            },
                            AddressLine = new AddressLineType[]
                            {
                                new AddressLineType
                                {
                                    Line =new LineType
                                    {
                                        Value= "carrera 8 Nº 6C - 39"
                                    }
                                }
                            },
                            Country = new CountryType
                            {
                                IdentificationCode = new IdentificationCodeType
                                {
                                    Value = "CO"
                                }
                            }
                        }
                    },
                    PartyTaxScheme = new PartyTaxSchemeType[]
                    {
                        new PartyTaxSchemeType
                        {
                            TaxLevelCode=new TaxLevelCodeType
                            {
                                Value="0"
                            }
                        }
                    },
                    Person = new PersonType
                    {
                        FirstName = new FirstNameType
                        {
                            Value = "Primer-N"
                        },
                        FamilyName = new FamilyNameType
                        {
                            Value = "Apellido-8355990"
                        },
                        MiddleName = new MiddleNameType
                        {
                            Value = "Segundo-N"
                        }
                    }
                }
            };

            factura.TaxTotal = new TaxTotalType1[]
            {
                new TaxTotalType1
                {
                    TaxAmount= new TaxAmountType
                    {
                        Value= 1619504.64m,
                        currencyID= CurrencyCodeContentType.COP
                    },
                    TaxEvidenceIndicator=new TaxEvidenceIndicatorType
                    {
                        Value=false
                    },
                    TaxSubtotal = new TaxSubtotalType1[]
                    {
                        new TaxSubtotalType1
                        {
                            TaxableAmount = new TaxableAmountType
                            {
                                Value=10121904,
                                currencyID = CurrencyCodeContentType.COP
                            },
                            TaxAmount = new TaxAmountType
                            {
                                Value= 1619504.64m,
                                currencyID= CurrencyCodeContentType.COP
                            },
                            Percent=new PercentType
                            {
                                Value=16
                            },
                            TaxCategory= new TaxCategoryType
                            {
                                TaxScheme=new TaxSchemeType
                                {
                                    ID=new IDType
                                    {
                                        Value="01"
                                    }
                                }
                            }
                        }
                    }                    
                },
                new TaxTotalType1
                {
                    TaxAmount=new TaxAmountType
                    {
                        Value=419046.82m,
                        currencyID=CurrencyCodeContentType.COP
                    },
                    TaxEvidenceIndicator=new TaxEvidenceIndicatorType
                    {
                        Value=false
                    },
                    TaxSubtotal=new TaxSubtotalType1[]
                    {
                        new TaxSubtotalType1
                        {
                            TaxableAmount= new TaxableAmountType
                            {
                                Value = 10121904,
                                currencyID = CurrencyCodeContentType.COP
                            },
                            TaxAmount = new TaxAmountType
                            {
                                currencyID=CurrencyCodeContentType.COP,
                                Value = 419046.82m
                            },
                            Percent = new PercentType
                            {
                                Value = 4.14m
                            },
                            //Anexo Técnico 001 Formatos de los Documentos XML de Facturación Electrónica Página 40 de 41
                            TaxCategory= new TaxCategoryType
                            {
                                TaxScheme=new TaxSchemeType
                                {
                                    ID=new IDType
                                    {
                                        Value ="03"
                                    }
                                }
                            }
                        }
                    }
                }
            };

            factura.LegalMonetaryTotal = new MonetaryTotalType
            {
                LineExtensionAmount = new LineExtensionAmountType
                {
                    currencyID = CurrencyCodeContentType.COP,
                    Value = 10121904
                },
                TaxExclusiveAmount = new TaxExclusiveAmountType
                {
                    currencyID = CurrencyCodeContentType.COP,
                    Value = 2038551.46m
                },
                PayableAmount=new PayableAmountType
                {
                    currencyID=CurrencyCodeContentType.COP,
                    Value= 12160455.46m
                }
            };

            factura.InvoiceLine = new InvoiceLineType[]
            {
                new InvoiceLineType
                {
                    ID=new IDType
                    {
                        Value="1"
                    },
                    InvoicedQuantity=new InvoicedQuantityType
                    {
                        Value=234
                    },
                    LineExtensionAmount=new LineExtensionAmountType
                    {
                         currencyID = CurrencyCodeContentType.COP,
                         Value= 10121904
                    },
                    Item=new ItemType
                    {
                        Description = new DescriptionType[]{
                            new DescriptionType
                            {
                                Value="Línea-1 4171263356 f-s0001_700085371_248c0_R545939-41-9715"
                            }
                        },                        
                    },
                    Price=new PriceType1
                    {
                        PriceAmount=new PriceAmountType
                        {
                            currencyID=CurrencyCodeContentType.COP,
                            Value=43256
                        }
                    }
                }
            };

            XmlSerializer facturaXML = new XmlSerializer(typeof(InvoiceType));

            var xmlFactura = "";
            XmlDocument facturaXMLDOC = new XmlDocument();
            using (var swf = new StringWriter())
            {
                using (XmlWriter writerf = XmlWriter.Create(swf))
                {
                    facturaXML.Serialize(writerf, factura);
                    xmlFactura = swf.ToString(); // Your XML
                }
                facturaXMLDOC.LoadXml(xmlFactura);
            }

            var facturaDeserializada = XmlDeserializeFromString(xmlFactura, typeof(InvoiceType));

            return facturaXMLDOC.InnerXml;
        }

For more info please click herehttps://stackoverflow.com/q/45745681



Have a question or need a custom quote?
info@smartinfocare.com