troubleshooting Question

Xml Add new node after specific node by element name

Avatar of conceptdata
conceptdataFlag for Denmark asked on
XMLC#LINQ Query
3 Comments1 Solution440 ViewsLast Modified:
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>



	<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>


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);

        }
    }
}


Can anyone help me out here - Thanks

/Kenenth
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros