Home > Enterprise >  How can I add prefixes to xml output using C#?
How can I add prefixes to xml output using C#?

Time:10-06

I wanna add prefixes like cbc: cac: iac: to xml output to see output like this - <cbc:ID>custom value</cbc:ID>  but  everytime it looks like <ID>custom value</ID>. Also I've used namespace urls in CreateElement method from XML Document but everytime the output is adding these urls after every prefix and I don't need this. Could u say to me how can I add prefixes in xml like that?

CodePudding user response:

If you are using XML serialization code will look like this. When no namespace use [XmlElement(Namespace = "")]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, "urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoice-1.0");
            namespaces.Add("cac", "urn:oasis:names:draft:ubl:schema:xsd:CommonAggregateComponents-2");
            namespaces.Add("cbc", "urn:oasis:names:draft:ubl:schema:xsd:CommonBasicComponents-2");
            namespaces.Add("ibc", "urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoiceBasicComponents-1.0");
            namespaces.Add("iac", "urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoiceAggregateComponents-1.0");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(DealerContractorInvoice));

            DealerContractorInvoice invoice = new DealerContractorInvoice();
            serializer.Serialize(writer, invoice, namespaces);
        }
    }
    [XmlRoot(Namespace = "urn:oasis:names:draft:ubl:schema:xsd:CommonBasicComponents-2")]
    public class DealerContractorInvoice
    {
        [XmlElement(Namespace = "urn: carrier:names: specification:ubl: schema:xsd: CarrierDealerContractorInvoiceAggregateComponents - 1.0")]
        public string ID { get; set; }

        [XmlElement(Namespace = "urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoiceAggregateComponents-1.0")]
        DealerContractorInvoiceLine DealerContractorInvoiceLine { get; set; }
    }
    public class DealerContractorInvoiceLine
    {
    }
}

Using xml linq :

       const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string ident = "<?xml version='1.0'?>"  
                           "<DealerContractorInvoice "  
                              " xmlns=\"urn: carrier: names: specification: ubl: schema: xsd: CarrierDealerContractorInvoice - 1.0\""  
                              " xmlns:cac=\"urn: oasis: names: draft: ubl: schema: xsd: CommonAggregateComponents - 2\""  
                              " xmlns:cbc=\"urn: oasis: names: draft: ubl: schema: xsd: CommonBasicComponents - 2\""  
                              " xmlns:ibc=\"urn: carrier: names: specification: ubl: schema: xsd: CarrierDealerContractorInvoiceBasicComponents - 1.0\""  
                              " xmlns:iac=\"urn: carrier: names: specification: ubl: schema: xsd: CarrierDealerContractorInvoiceAggregateComponents - 1.0\""  
                              " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""  
                              " xsi:schemaLocation=\"urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoice-1.0 ../../src/xsd/runtime/maindoc/UBL-CarrierDealerContractorInvoice-1.0.xsd\""  
                           "/>";

            XDocument doc = XDocument.Parse(ident);
            XElement invoice = doc.Root;

            //use when no prefix
            XNamespace ns = invoice.GetDefaultNamespace();
            XNamespace nsCbc = invoice.GetNamespaceOfPrefix("cbc");

            XElement id = new XElement(nsCbc   "ID");
            invoice.Add(id);
        }

CodePudding user response:

This is an xml which i wanna see in output.

- <DealerContractorInvoice xmlns="urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoice-1.0" xmlns:cac="urn:oasis:names:draft:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:draft:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ibc="urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoiceBasicComponents-1.0" xmlns:iac="urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoiceAggregateComponents-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:carrier:names:specification:ubl:schema:xsd:CarrierDealerContractorInvoice-1.0 ../../src/xsd/runtime/maindoc/UBL-CarrierDealerContractorInvoice-1.0.xsd">
  <cbc:ID>690336</cbc:ID> 
  <cbc:IssueDate>2021-07-09</cbc:IssueDate> 
- <cac:BuyerCustomerParty>
  <cbc:SupplierAssignedAccountID>10016</cbc:SupplierAssignedAccountID> 
  </cac:BuyerCustomerParty>
- <cac:SellerSupplierParty>
  <cbc:SupplierAssignedAccountID>CARRIER</cbc:SupplierAssignedAccountID> 
  </cac:SellerSupplierParty>
- <cac:DeliveryAddress>
  <cbc:ID>HIG060</cbc:ID> 
  <cbc:PostalZone>12831</cbc:PostalZone> 
  </cac:DeliveryAddress>
- <!-- ************** end of header  **********
  --> 
- <iac:DealerContractorInvoiceLine>
  <cbc:ID>016</cbc:ID> 
  <cbc:Quantity>10</cbc:Quantity> 
- <cac:AccountingDocumentReference>
- <cac:AdditionalDocumentReference>
  <cbc:ID>464299_016</cbc:ID> 
  <cbc:CopyIndicator>false</cbc:CopyIndicator> 
  </cac:AdditionalDocumentReference>
  </cac:AccountingDocumentReference>
- <cac:QuotationLineReference>
  <cbc:LineID>20</cbc:LineID> 
- <cac:DocumentReference>
  <cbc:ID>7114494</cbc:ID> 
  <cbc:CopyIndicator>false</cbc:CopyIndicator> 
  </cac:DocumentReference>
  </cac:QuotationLineReference>
- <cac:Delivery>
- <cac:OriginatorParty>
- <cac:PartyIdentification>
  <cbc:ID>999999</cbc:ID> 
  </cac:PartyIdentification>
  </cac:OriginatorParty>
- <cac:Despatch>
  <cbc:ActualDespatchDateTime>2021-07-09T00:00:00-05:00</cbc:ActualDespatchDateTime> 
  </cac:Despatch>
  </cac:Delivery>
- <iac:Transfer>
  <ibc:TransferTypeCode>SS</ibc:TransferTypeCode> 
  <ibc:TransferPartnerCode>DC</ibc:TransferPartnerCode> 
  </iac:Transfer>
- <cac:Item>
  <cbc:HazardousRiskIndicator>false</cbc:HazardousRiskIndicator> 
  <cbc:BrandName>CARRIER</cbc:BrandName> 
- <cac:SellersItemIdentification>
  <cbc:ID>CNPVP1814ALA</cbc:ID> 
  </cac:SellersItemIdentification>
- <!-- ************** end of detl  **********
  --> 
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>1724141X44098</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>172441X44106</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>1721777X44113</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>1721X44129</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>17744121X44147</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>1744721X44160</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>4244720X56204</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>423420X56600</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>488420X58936</cbc:SerialID> 
  </cac:ItemInstance>
- <cac:ItemInstance>
  <cbc:ProductTraceID /> 
  <cbc:SerialID>44202X58948</cbc:SerialID> 
  </cac:ItemInstance>
  </cac:Item>
  <ibc:ActualExtendedSellAmount currencyID="USD">1420.00</ibc:ActualExtendedSellAmount> 
  <ibc:ReverseFlowIndicator>false</ibc:ReverseFlowIndicator> 
  <ibc:PartSurplusIndicator>false</ibc:PartSurplusIndicator> 
  </iac:DealerContractorInvoiceLine>
  </DealerContractorInvoice>
  • Related