Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

XML node passed to function

Closed Thread
 
Thread Tools Display Modes
Old 07-10-2008   #1 (permalink)
David Trimboli
Guest


 

XML node passed to function

I'm working on a script that converts a specific format of XML file to a
CSV document. I'm getting stuck at a point where I pass an object of
type System.Xml.XmlElement to a function, but the function receives an
object of type System.Object[] instead. I've confirmed with debugging
that these types are, in fact, what are being passed and received. I
can't figure out why the variable gets changed to System.Object[] by the
time it gets into the function.

I've tried building other XML-parsing scripts that only deal with
passing XML variables to functions, and these scripts work correctly.
They do not change the object type before it enters the function.

Can someone look at this script and tell me why the GetHeaders function
thinks it's getting a $XMLnode variable of type System.Object[] instead
of System.Xml.XmlElement?


param ($InputXML = (throw "You must specify an input XML file."))

function GetHeaders ($XMLnode, $RecordType)
{
if ($XMLnode -is "System.Xml.XmlText")
{
Add-Content -Path $RecordType + "s.csv" -Value $XMLnode.get_Name()
}
else
{
foreach ($ChildNode in $XMLnode.get_ChildNodes())
{
GetHeaders($ChildNode, $RecordType)
}
}
}

function GetData ($XMLnode, $RecordType)
{
if ($XMLnode -is "System.Xml.XmlText")
{
Add-Content -Path $RecordType + "s.csv" -Value $XMLnode.get_InnerText()
}
else
{
foreach ($ChildNode in $XMLnode.get_ChildNodes())
{
GetHeaders($ChildNode, $RecordType)
}
}
}

[xml] $xml = Get-Content -Path $InputXML
$RequisitionCandidates = $xml.VisionDataExchange.ExportData.get_ChildNodes()
foreach ($RequisitionCandidate in $RequisitionCandidates)
{
GetHeaders($RequisitionCandidate.Candidate, "Candidate")
Add-Content -Path Candidates.csv -Value "`n"
GetData($RequisitionCandidate.Candidate, "Candidate")
Add-Content -Path Candidates.csv -Value "`n"
GetHeaders($RequisitionCandidate.Requisition, "Requisition")
Add-Content -Path Requisitions.csv -Value "`n"
GetData($RequisitionCandidate.Requisition, "Requisition")
Add-Content -Path Requisitions.csv -Value "`n"
}


--
David
Stardate 8525.2
Old 07-10-2008   #2 (permalink)
Oisin (x0n) Grehan [MVP]
Guest


 

Re: XML node passed to function

On Jul 10, 4:21*pm, David Trimboli <trimb...@xxxxxx> wrote:
Quote:

> I'm working on a script that converts a specific format of XML file to a
> CSV document. I'm getting stuck at a point where I pass an object of
> type System.Xml.XmlElement to a function, but the function receives an
> object of type System.Object[] instead. I've confirmed with debugging
> that these types are, in fact, what are being passed and received. I
> can't figure out why the variable gets changed to System.Object[] by the
> time it gets into the function.
>
> I've tried building other XML-parsing scripts that only deal with
> passing XML variables to functions, and these scripts work correctly.
> They do not change the object type before it enters the function.
>
> Can someone look at this script and tell me why the GetHeaders function
> thinks it's getting a $XMLnode variable of type System.Object[] instead
> of System.Xml.XmlElement?
>
> param ($InputXML = (throw "You must specify an input XML file."))
>
> function GetHeaders ($XMLnode, $RecordType)
> {
> * * *if ($XMLnode -is "System.Xml.XmlText")
> * * *{
> * * * * Add-Content -Path $RecordType + "s.csv" -Value $XMLnode.get_Name()
> * * *}
> * * *else
> * * *{
> * * * * foreach ($ChildNode in $XMLnode.get_ChildNodes())
> * * * * {
> * * * * * * GetHeaders($ChildNode, $RecordType)
> * * * * }
> * * *}
>
> }
>
> function GetData ($XMLnode, $RecordType)
> {
> * * *if ($XMLnode -is "System.Xml.XmlText")
> * * *{
> * * * * Add-Content -Path $RecordType + "s.csv" -Value $XMLnode.get_InnerText()
> * * *}
> * * *else
> * * *{
> * * * * foreach ($ChildNode in $XMLnode.get_ChildNodes())
> * * * * {
> * * * * * * GetHeaders($ChildNode, $RecordType)
> * * * * }
> * * *}
>
> }
>
> [xml] $xml = Get-Content -Path $InputXML
> $RequisitionCandidates = $xml.VisionDataExchange.ExportData.get_ChildNodes()
> foreach ($RequisitionCandidate in $RequisitionCandidates)
> {
> * * *GetHeaders($RequisitionCandidate.Candidate, "Candidate")
> * * *Add-Content -Path Candidates.csv -Value "`n"
> * * *GetData($RequisitionCandidate.Candidate, "Candidate")
> * * *Add-Content -Path Candidates.csv -Value "`n"
> * * *GetHeaders($RequisitionCandidate.Requisition, "Requisition")
> * * *Add-Content -Path Requisitions.csv -Value "`n"
> * * *GetData($RequisitionCandidate.Requisition, "Requisition")
> * * *Add-Content -Path Requisitions.csv -Value "`n"
>
> }
>
> --
> David
> Stardate 8525.2
Hi David,

This is a common source of confusion in powershell. .NET methods use
brackets and comma separated arguments, but powershell functions do
not. Case in point:
Quote:

> function func ($a, $b) { "a: $a ; b: $b" }
> func 1 2
a: 1 ; b: 2
Quote:

> func(1,2)
a: 1 2 ; b:

As you can see, $a is object[] with two elements: 1 & 2.

So, in short you should space separate your function arguments.

- Oisin
PowerShell MVP
http://www.nivot.org/
Old 07-11-2008   #3 (permalink)
David Trimboli
Guest


 

Re: XML node passed to function

Oisin (x0n) Grehan [MVP] wrote:
Quote:

> On Jul 10, 4:21 pm, David Trimboli <trimb...@xxxxxx> wrote:
Quote:

>> I'm working on a script that converts a specific format of XML file to a
>> CSV document. I'm getting stuck at a point where I pass an object of
>> type System.Xml.XmlElement to a function, but the function receives an
>> object of type System.Object[] instead.
Quote:

>% function func ($a, $b) { "a: $a ; b: $b" }
>% func 1 2
> a: 1 ; b: 2
>% func(1,2)
> a: 1 2 ; b:
>
> As you can see, $a is object[] with two elements: 1 & 2.
>
> So, in short you should space separate your function arguments.
Oh, god. I knew that. Argh! (I mean, arg!)

Thanks, Oisin.

--
David
Stardate 8527.1
Old 07-15-2008   #4 (permalink)
Martin Zugec
Guest


 

Re: XML node passed to function

Hehe, this still happens to me even today Having IDE that will have
special highlight whenever you use () with functions would save me lot of
time

Martin


"David Trimboli" <trimboli@xxxxxx> wrote in message
news:ea9mmi14IHA.4272@xxxxxx
Quote:

> Oisin (x0n) Grehan [MVP] wrote:
Quote:

>> On Jul 10, 4:21 pm, David Trimboli <trimb...@xxxxxx> wrote:
Quote:

>>> I'm working on a script that converts a specific format of XML file to a
>>> CSV document. I'm getting stuck at a point where I pass an object of
>>> type System.Xml.XmlElement to a function, but the function receives an
>>> object of type System.Object[] instead.
>
Quote:

>>% function func ($a, $b) { "a: $a ; b: $b" }
>>% func 1 2
>> a: 1 ; b: 2
>>% func(1,2)
>> a: 1 2 ; b:
>>
>> As you can see, $a is object[] with two elements: 1 & 2.
>>
>> So, in short you should space separate your function arguments.
>
> Oh, god. I knew that. Argh! (I mean, arg!)
>
> Thanks, Oisin.
>
> --
> David
> Stardate 8527.1
Old 07-15-2008   #5 (permalink)
David Trimboli
Guest


 

Re: XML node passed to function

Martin Zugec wrote:
Quote:

> "David Trimboli" <trimboli@xxxxxx> wrote in message
> news:ea9mmi14IHA.4272@xxxxxx
Quote:

>> Oisin (x0n) Grehan [MVP] wrote:
Quote:

>>> On Jul 10, 4:21 pm, David Trimboli <trimb...@xxxxxx> wrote:
>>>> I'm working on a script that converts a specific format of XML file
>>>> to a
>>>> CSV document. I'm getting stuck at a point where I pass an object of
>>>> type System.Xml.XmlElement to a function, but the function receives an
>>>> object of type System.Object[] instead.
>>
Quote:

>>> % function func ($a, $b) { "a: $a ; b: $b" }
>>> % func 1 2
>>> a: 1 ; b: 2
>>> % func(1,2)
>>> a: 1 2 ; b:
>>>
>>> As you can see, $a is object[] with two elements: 1 & 2.
>>>
>>> So, in short you should space separate your function arguments.
>>
>> Oh, god. I knew that. Argh! (I mean, arg!)
>
> Hehe, this still happens to me even today Having IDE that will have
> special highlight whenever you use () with functions would save me lot
> of time
I think from now on I'm going to name my functions just like cmdlets so
that I treat them the same way...

--
David
Stardate 8538.2
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shouldn't this give me the active node on the cluster? akcorr PowerShell 1 05-19-2008 12:47 PM
Removing an XML node Leon Mayne .NET General 1 02-27-2008 10:04 AM
How to delete a DFS node using Powershell Richard Adams PowerShell 1 02-19-2008 08:03 PM
How many parameters ($args) can be passed to a function? Kryten PowerShell 7 11-06-2007 11:56 PM
Counting a particular node in a XML file Nikhil R. Bhandari PowerShell 0 04-03-2007 03:06 AM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50