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
conceptdataAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Kenenth;

Please modify your code as follows and it will give you what you need.
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")));

// Get a reference to the node which you want to insert the new node after
XElement insertAfter = xmlDoc.Element(cac + "OrderReference");
// Test to make sure the node insertAfter was found before insetting
if(insertAfter != null) 
{
    insertAfter.AddAfterSelf(newQuery);
    // Save the modified document to the file system
    xmlDoc.Save(resultfilename);
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
conceptdataAuthor Commented:
Perfect.
Thanks a lot.. :)
Fernando SotoRetiredCommented:
Not a problem Kenenth, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.