0

I want to add existing XML nodes from one file into another XML file by using PowerShell. (Edit the Web.config from some webservices). I tried many ways but I don`t get it running, any help would be appreciated!

My File1 contains some code like:

<?xml version="1.0"?>
<services>
  <service name="MyService.HelloWorld">
    <endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloWorld/" contract="MyService.HelloWorld" />
  </service>
  <service name="MyService.HelloMars">
    <endpoint binding="wsHttpBinding" bindingConfiguration="NewBinding0" bindingNamespace="http://tempuri.org/HelloMars/" contract="MyService.HelloMars" />
  </service>
  .........

Now I want to add all the services into another xml file (the Web.config) and there into a specific node.

My File2 contains some code like:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
	<configSections>
		<sectionGroup name="applicationSettings"
			type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
			<section name="MyServices.Properties.Settings"
				type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
		</sectionGroup>
	</configSections>
	<system.web>
		<compilation debug="true" targetFramework="4.0"/>
	</system.web>
	<system.serviceModel>

  ............ (some more code)
      
		<diagnostics>
			<messageLogging logEntireMessage="true" logMalformedMessages="true"       logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
				maxMessagesToLog="2000"/>
		</diagnostics>
		<services>

		**(HERE I WANT TO ADD MY SERVICES!)**
			
		</services>      
  

I also need to modify the imported services, by adding them an extra parameter (Endpoint), so that they look like this: (adding the address parameter)

<service name="MyServices.HelloWorld">
<endpoint binding="wsHttpBinding" bindingConfiguration="NewWsHttpBinding"   bindingNamespace="http://tempuri.org/HelloWorld/"
address="https://...../HelloWorld.svc" contract="MyServices.HelloWorld"/>
</service>

I tried many ways I found on the internet, but none worked. The last thing I tried to at least merge the files was:

$webConfig = [xml](Get-Content C:\...\Web.config)
$webConfigNode = $webConfig.configuration.system.serviceModel.services
$servicesToAdd = [xml](Get-Content C:\...\services.config)
$servicesToAddNode = $servicesToAdd.services

while ($servicesToAddNode.HasChildNodes)
{
    $cn = $servicesToAddNode.FirstChild
    $cn = $servicesToAddNode.RemoveChild($cn)
    $cn = $webConfigNode.OwnerDocument.importnode($cn)
    $webConfigNode.AppendChild($cn)
}

but I get the following error message:

It is not possible to run a method for an expression which is null
+     $cn = $webConfig.OwnerDocument.importnode($cn)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Thanks for any help!

1 Answer 1

0

Obviously too late for you, but for anyone else, the short version is that in .NET's XML Document model, nodes belong to their document, so you have to ...

  • FIRST call Document.ImportNode to clone a node into a new document
  • THEN actually insert or append the imported node somewhere

For example, if you:

$Source = [xml]'<root><a><s name="one"/><s name="two"/></a></root>'
$Dest = [xml]'<root><b><s name="three"/></b></root>'
foreach($node in $Source.root.a.s) {
    $clone = $Dest.ImportNode($node, $true)
    $null = $Dest.root.b.AppendChild($clode)
}

Then, your $Dest.OuterXml will be:

<root><b><s name="three" /><s name="one" /><s name="two" /></b></root>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.