<JEDevParams>
<Login>
<AMServer></AMServer>
<NRCSServer></NRCSServer>
<User></User>
<Password></Password> // only used for autotest
</Login>
<NRCS>
<Version>2.1.1.20</Version>
<SearchLimit>300</SearchLimit>
<SkipFolderCount>yes</SkipFolderCount> // yes = don't get count for
browser folders
</NRCS>
</JEDevParams>
Here is what I thought would work:
[xml] $xml = get-content "F:\Scripts\Test\JEDevParams.xml"
$rootParams = $xml.SelectSingleNode("//JEDevParams")
#$rootParams.NRCS.Version==
$nodes = $rootParams.SelectNodes("NRCS")
for ($i=0; $i -lt $nodes.Count; $i++)
{
if ($nodes[$i].SelectSingleNode("Version").InnerText -eq "2.1.1.20")
{
$rootParams.RemoveChild($nodes[$i])
}
}
$xml.Save("F:\Test\JEDevResult.xml")
You're close. Try this:
PS> [xml]$xml = @'
>> <JEDevParams>
>> <Login>
>> <AMServer></AMServer>
>> <NRCSServer></NRCSServer>
>> <User></User>
>> <Password></Password> // only used for autotest
>> </Login>
>> <NRCS>
>> <Version>2.1.1.20</Version>
>> <SearchLimit>300</SearchLimit>
>> <SkipFolderCount>yes</SkipFolderCount> // yes = don't get count for
>> browser folders
>> </NRCS>
>> </JEDevParams>
>> '@
>>
PS> $xml | fxml
<JEDevParams>
<Login>
<AMServer></AMServer>
<NRCSServer></NRCSServer>
<User></User>
<Password></Password> // only used for autotest
</Login>
<NRCS>
<Version>2.1.1.20</Version>
<SearchLimit>300</SearchLimit>
<SkipFolderCount>yes</SkipFolderCount> // yes = don't get count for
browser folders
</NRCS>
</JEDevParams>
PS> $xml.JEDevParams.NRCS.RemoveChild($xml.SelectSingleNode('//Version'))
#text
-----
2.1.1.20
PS> $xml | fxml
<JEDevParams>
<Login>
<AMServer></AMServer>
<NRCSServer></NRCSServer>
<User></User>
<Password></Password> // only used for autotest
</Login>
<NRCS>
<SearchLimit>300</SearchLimit>
<SkipFolderCount>yes</SkipFolderCount> // yes = don't get count for
browser folders
</NRCS>
</JEDevParams>
Note that fxml (format-xml) is a cmdlet provide by the PowerShell Community
Extensions.
--
Keith Hill
http://www.codeplex.com/powershellcx
I think I found a limitation/bug in format-xml. When I was playing around
with it this afternoon, I was a bit confused because it was not formatting
the XML from my source file very well. I simplifieed the source file (eg
removed comments) and it worked fine. I went back and added some comments and
found that the function works fine when the comments are in some contexts
but not others.
For example, the format-xml will format contains lines like the following
line without a problem:
...
<NRCSServer></NRCSServer> // Comment 2
...
However, the formatting gets gummed up with the following:
<?xml version="1.0" encoding="UTF8"?>
<JEDevParams>
//Comment
//Comment
<Login>...
FYI the "// Comment 2" would be considered text within XML. XML comments
are delineated like so:
<!-- comment here -->
The two "//Comment" lines above would be considered text nodes within the
root element JEDevParams. But I do see what you are saying. The presence
of text nodes is messing up the formatting. I'll take a look at that.
Thanks,
Keith
-Ryan Irwin
<?xml version="1.0" encoding="utf-8" ?>
<Addresses version="1.0">
<Contacts>
<Contact name="ABC Company" type="business">
<Street>123 Anywhere St</Street>
<City>Someplace</City>
<State>AA</State>
<Zip>12345</Zip>
<Phone>555-123-4567</Phone>
</Contact>
<Contact name="John Doe" type="personal">
<Phone>555-333-4444</Phone>
</Contact>
</Contacts>
</Addresses>
I would like to save the contact node based on the type of personal, delete
any other node whose type does not match personal.
What I can't seem to figure out is how to drill down to the "type" level.
$xmldata= [xml] (Get-Content .\test.xml)
$xmldata.SelectNodes("//Contact[@type!=`"personal`"]") | % {
$xmldata.SelectNodes("//Contacts").item(0).removechild($_)}
$xmldata.save((pwd).path+"\nuevo.xml")
The key in this is the string writed in xpath....you can find examples for
xpath here http://msdn.microsoft.com/en-us/library/ms256086.aspx
Good luck.
Michael Soza.
"Ryan Irwin" <Ryan...@discussions.microsoft.com> wrote in message
news:29BBFB3E-C319-484C...@microsoft.com...
Thank you so much, that was exactly what I needed, now to reverse engineer
it so I better understand what it's doing and how I can use it in other
applications. (Total PS newbie here).
Thanks again,
Ryan
"Ryan Irwin" <Ryan...@discussions.microsoft.com> wrote in message
news:0FCB7114-439B-453C...@microsoft.com...