Link to home
Start Free TrialLog in
Avatar of conceptdata
conceptdataFlag for Denmark

asked on

Xml Add new node after specific node by element name

Hi

I wants til insert a xml node/node with child into an existing XML fil.
But I need it to be after a specific node :) -  "OrderReference"

The Existing File looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href=""?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:oasis:names:specification:ubl:schema:xsd:CoreComponentParameters-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:sdt="urn:oasis:names:specification:ubl:schema:xsd:SpecializedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.0.xsd">
	<ext:UBLExtensions>
		<ext:UBLExtension>
			<cbc:ID>1</cbc:ID>
			<ext:ExtensionAgencyID>XX</ext:ExtensionAgencyID>
			<ext:ExtensionAgencyURI/>
			<ext:ExtensionContent>
				<EG>
					<EGBIdent></EGBIdent>
					<EGStatus></EGStatus>
					<EGVersion></EGVersion>
					<EGValdat></EGValdat>
					<EGAfdeling></EGAfdeling>
					<EGSupplier>
						<cbc:ID schemeID=""></cbc:ID>
					</EGSupplier>
				</EG>
			</ext:ExtensionContent>
		</ext:UBLExtension>
	</ext:UBLExtensions>
	<cbc:UBLVersionID></cbc:UBLVersionID>
	<cbc:CustomizationID>OIOUBL-2.02</cbc:CustomizationID>
	<cbc:ProfileID schemeAgencyID="320" schemeID="urn:oioubl:id:profileid-1.2">urn:www.nesubl.eu:profiles:profile5:ver2.0</cbc:ProfileID>
	<cbc:ID></cbc:ID>
	<cbc:CopyIndicator>false</cbc:CopyIndicator>
	<cbc:IssueDate>2017-09-05</cbc:IssueDate>
	<cbc:InvoiceTypeCode listAgencyID="" listID="urn:oioubl:codelist:invoicetypecode-1.1"></cbc:InvoiceTypeCode>
	<cbc:Note>0</cbc:Note>
	<cbc:DocumentCurrencyCode>DKK</cbc:DocumentCurrencyCode>
	<cbc:AccountingCost/>
	<cac:OrderReference>
		<cbc:ID>null</cbc:ID>
		<cbc:IssueDate>2017-09-05</cbc:IssueDate>
	</cac:OrderReference>
	<cac:AccountingSupplierParty>
		<cac:Party>
			<cbc:EndpointID schemeID="DK:CVR"></cbc:EndpointID>
			<cac:PartyIdentification>
				<cbc:ID schemeID="DK:CVR"></cbc:ID>
			</cac:PartyIdentification>
			<cac:PartyName>
				<cbc:Name></cbc:Name>
			</cac:PartyName>
			<cac:PostalAddress>
				<cbc:AddressFormatCode listAgencyID="320" listID="urn:oioubl:codelist:addressformatcode-1.1">StructuredLax</cbc:AddressFormatCode>
				<cbc:StreetName></cbc:StreetName>
				<cbc:BuildingNumber>36</cbc:BuildingNumber>
				<cbc:MarkCare></cbc:MarkCare>
				<cbc:CityName></cbc:CityName>
				<cbc:PostalZone></cbc:PostalZone>
				<cac:Country>
					<cbc:IdentificationCode></cbc:IdentificationCode>
				</cac:Country>
			</cac:PostalAddress>
			<cac:PartyTaxScheme>
				<cbc:CompanyID schemeID="DK:SE"></cbc:CompanyID>
				<cac:TaxScheme>
					<cbc:ID schemeAgencyID="320" schemeID="urn:oioubl:id:taxschemeid-1.1">63</cbc:ID>
					<cbc:Name></cbc:Name>
				</cac:TaxScheme>
			</cac:PartyTaxScheme>
			<cac:PartyLegalEntity>
				<cbc:RegistrationName></cbc:RegistrationName>
				<cbc:CompanyID schemeID="DK:CVR"></cbc:CompanyID>
			</cac:PartyLegalEntity>
		</cac:Party>
	</cac:AccountingSupplierParty>
</Invoice>

Open in new window




	<cac:AdditionalDocumentReference>
		<cbc:ID></cbc:ID>
		<cbc:CopyIndicator>true</cbc:CopyIndicator>
		<cbc:DocumentTypeCode listAgencyName=""></cbc:DocumentTypeCode>
		<cac:Attachment>
			<cbc:EmbeddedDocumentBinaryObject characterSetCode="UTF-8" encodingCode="Base64" mimeCode="image/tiff">

			</cbc:EmbeddedDocumentBinaryObject>
		</cac:Attachment>
	</cac:AdditionalDocumentReference>

Open in new window



Now I have this code, but it inserts it last.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Xml.XPath;

namespace Test_XML
{
    class Program
    {
        static void Main(string[] args)
        {

            var filename = @"D:\TEMP\OiOUbl\Test.xml";
            var resultfilename = @"D:\TEMP\OiOUbl\TestResult.xml";

            XNamespace cac = "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2";
            XNamespace cbc = "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2";

            XElement xmlDoc = XElement.Load(filename);
            XNamespace ns = ((XElement)xmlDoc.FirstNode).GetDefaultNamespace();

            XElement newQuery =
              new XElement(cac + "AdditionalDocumentReference",
                new XElement(cac + "Attachment",
                    new XElement(cbc + "EmbeddedDocumentBinaryObject")));

            xmlDoc.Add(newQuery);
            xmlDoc.Save(resultfilename);

        }
    }
}

Open in new window



Can anyone help me out here - Thanks

/Kenenth
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of conceptdata

ASKER

Perfect.
Thanks a lot.. :)
Not a problem Kenenth, glad to help.