1

I have made a function for creating new xml node.there are two parameters in my function on is a existing xml file reference and second one is element value.while running the script its showing an error

enter image description here

code

function createProviderNode($xmlData,$propertyValue){
Write-Host 'inside createProviderNode'
Write-Host ($propertyValue)
#[xml]$xmlData = get-content E:\powershell\data.xml
$newProviderNode = $xmlData.CreateNode("element","provider","")
$newProviderNode.SetAttribute("name",$propertyValue)
$xmlData.SelectSingleNode('providers').AppendChild($newProviderNode)
$xmlData.save("E:\powershell\data.xml")

}

did i miss anything in this code?

0

2 Answers 2

1

The error message implies that while you expected $xmlData to contain an object of type [xml] (System.Xml.XmlDocument) - i.e., an XML document - in reality it was a string ([string]).

In other words: When you called your createProviderNode function, the 1st argument you passed was a string, not an XML document (of type [xml]).

Typing your $xmlData parameter variable as [xml] solves this problem, as that will implicitly covert even a string argument to an XML document on demand - if possible.

A simplified example, using a script block in lieu of a function:

$xmlString = @'
<?xml version="1.0"?><catalog><book id="bk101"><title>De Profundis</title></book></catalog>
'@

# Note how $xmlData is [xml]-typed.
& { param([xml] $xmlData) $xmlData.catalog.book.title } $xmlString

The above yields De Profundis, demonstrating that the string argument was converted to an [xml] instance (which - thanks to PowerShell's type adaptation magic - makes the element names available as direct properties). It is then safe to call the .CreateNode() method on $xmlData.

Sign up to request clarification or add additional context in comments.

Comments

1

Well, you don't show your original XML format. Why did you comment out that Get-Content? it will not work without it.

So, if we take the below example, it works as expected.

# Simple XML version

$SimpleXml = $null

$SimpleXml = @"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <name>Apple</name>
    <size>1234</size>
</configuration>
"@


# New node code
[xml]$XmlDoc = Get-Content -Path variable:\SimpleXml

$runtime = $XmlDoc.CreateNode("element","runtime","")

$generated = $XmlDoc.CreateNode("element","generatePublisherEvidence","")
$generated.SetAttribute("enabled","false")

$runtime.AppendChild($generated)

$XmlDoc.configuration.AppendChild($runtime)

$XmlDoc.save("$pwd\SimpleXml.xml")
Get-Content -Path "$pwd\SimpleXml.xml"


# Which creates this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <name>Apple</name>
  <size>1234</size>
  <runtime>
    <generatePublisherEvidence enabled="false" />
  </runtime>
</configuration>

Also Write-Host is never needed unless you are coloring screen output. Write-Output is the default and automatically write to the screen, whether you specify Write-Output or not.

So, all of these to the same thing - output to the screen.

$SomeString = 'hello'
Write-Host $SomeString
Write-Output $SomeString

'hello'
"hello"
$SomeString
"$SomeString"
($SomeString)
("$SomeString")
$($SomeString)

# Results

hello
hello
hello
hello
hello
hello
hello

… yet, it's your choice.

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.