Populating XML data

Hi Guys, I'm fairly new to Powershell (and XML) and have come across a
stumbling block that I can't resolve.

I'm trying to write some code to poulate an XML file with data about
sites within our organisation. This issue is with the nested nodes (I
think they're called nodes?)

The XML which I hand wrote looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Organisation>
<Site>
<Code>MEL</Code>
<Name>Melbourne</Name>
<MailServer>MAILSRV1</MailServer>
<State>VIC</State>
<SecurityGroups>
<group>SecGroup1</group>
<group>SecGroup2</group>
</SecurityGroups>
</Site>
</Organisation>

I can read this in to my script and work with it fine. The issue is
with trying to pragmatically populate it. Using this code I can create a
new site with no issues with the code below but no matter how hard I
search I can't work out how to enter multiple child nodes in the
<SecurityGroups> tag.

$XML = New-Object XML
$xml.Load("c:\sites.xml")
$sampleSite = @($xml.Organisation.Site)[0]
$newSite = $sampleSite.Clone()
$newSite.Code = "SYD"
$newSite.Name = "Sydney"
$newSite.MailServer = "MAILSRV2"
$newSite.State = "NSW"
$xml.Organisation.AppendChild($newSite)
$xml.Save("c:\sites.xml")

This is messy I know but it clones the existing xml file where I update
the info, append the data and save. Any suggestions on a better way to
do this would be great too.

How can I add multiple new items into the <SecurityGroups> node? I know
I'm missing something fundamental here but it's driving me crazy :D Any
help would be rally appreciated.


-- 
Pete1981
0
Pete1981
2/28/2010 9:37:22 PM
windows.powershell 690 articles. 0 followers. Follow

2 Replies
1194 Views

Similar Articles

[PageSpeed] 51

I struggled with that one for a while to.  I'm not sure if there's a better 
way, but this will work for you.  I tested it:

$newgroup = $xml.CreateElement("group")
$newgroup.set_innerXML("SecGroup3")
$newsite.SecurityGroups.AppendChild($newgroup)

You need to add the above before you do the main 
$xml.organisation.appendchhile()


"Pete1981" wrote:

> 
> Hi Guys, I'm fairly new to Powershell (and XML) and have come across a
> stumbling block that I can't resolve.
> 
> I'm trying to write some code to poulate an XML file with data about
> sites within our organisation. This issue is with the nested nodes (I
> think they're called nodes?)
> 
> The XML which I hand wrote looks like this:
> 
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <Organisation>
> <Site>
> <Code>MEL</Code>
> <Name>Melbourne</Name>
> <MailServer>MAILSRV1</MailServer>
> <State>VIC</State>
> <SecurityGroups>
> <group>SecGroup1</group>
> <group>SecGroup2</group>
> </SecurityGroups>
> </Site>
> </Organisation>
> 
> I can read this in to my script and work with it fine. The issue is
> with trying to pragmatically populate it. Using this code I can create a
> new site with no issues with the code below but no matter how hard I
> search I can't work out how to enter multiple child nodes in the
> <SecurityGroups> tag.
> 
> $XML = New-Object XML
> $xml.Load("c:\sites.xml")
> $sampleSite = @($xml.Organisation.Site)[0]
> $newSite = $sampleSite.Clone()
> $newSite.Code = "SYD"
> $newSite.Name = "Sydney"
> $newSite.MailServer = "MAILSRV2"
> $newSite.State = "NSW"
> $xml.Organisation.AppendChild($newSite)
> $xml.Save("c:\sites.xml")
> 
> This is messy I know but it clones the existing xml file where I update
> the info, append the data and save. Any suggestions on a better way to
> do this would be great too.
> 
> How can I add multiple new items into the <SecurityGroups> node? I know
> I'm missing something fundamental here but it's driving me crazy :D Any
> help would be rally appreciated.
> 
> 
> -- 
> Pete1981
> .
> 
0
Utf
3/1/2010 4:19:01 AM
I forgot to mention yesterday that the set_innerxml is a hidden member.  For 
some reason a bunch of the methods are hidden in the xml objects.  You can 
see them with this:
$newgroup |get-member -force

"Tome Tanasovski" wrote:

> I struggled with that one for a while to.  I'm not sure if there's a better 
> way, but this will work for you.  I tested it:
> 
> $newgroup = $xml.CreateElement("group")
> $newgroup.set_innerXML("SecGroup3")
> $newsite.SecurityGroups.AppendChild($newgroup)
> 
> You need to add the above before you do the main 
> $xml.organisation.appendchhile()
> 
> 
> "Pete1981" wrote:
> 
> > 
> > Hi Guys, I'm fairly new to Powershell (and XML) and have come across a
> > stumbling block that I can't resolve.
> > 
> > I'm trying to write some code to poulate an XML file with data about
> > sites within our organisation. This issue is with the nested nodes (I
> > think they're called nodes?)
> > 
> > The XML which I hand wrote looks like this:
> > 
> > <?xml version="1.0" encoding="ISO-8859-1"?>
> > <Organisation>
> > <Site>
> > <Code>MEL</Code>
> > <Name>Melbourne</Name>
> > <MailServer>MAILSRV1</MailServer>
> > <State>VIC</State>
> > <SecurityGroups>
> > <group>SecGroup1</group>
> > <group>SecGroup2</group>
> > </SecurityGroups>
> > </Site>
> > </Organisation>
> > 
> > I can read this in to my script and work with it fine. The issue is
> > with trying to pragmatically populate it. Using this code I can create a
> > new site with no issues with the code below but no matter how hard I
> > search I can't work out how to enter multiple child nodes in the
> > <SecurityGroups> tag.
> > 
> > $XML = New-Object XML
> > $xml.Load("c:\sites.xml")
> > $sampleSite = @($xml.Organisation.Site)[0]
> > $newSite = $sampleSite.Clone()
> > $newSite.Code = "SYD"
> > $newSite.Name = "Sydney"
> > $newSite.MailServer = "MAILSRV2"
> > $newSite.State = "NSW"
> > $xml.Organisation.AppendChild($newSite)
> > $xml.Save("c:\sites.xml")
> > 
> > This is messy I know but it clones the existing xml file where I update
> > the info, append the data and save. Any suggestions on a better way to
> > do this would be great too.
> > 
> > How can I add multiple new items into the <SecurityGroups> node? I know
> > I'm missing something fundamental here but it's driving me crazy :D Any
> > help would be rally appreciated.
> > 
> > 
> > -- 
> > Pete1981
> > .
> > 
0
Utf
3/1/2010 1:27:01 PM
Reply:

Similar Artilces:

Opening Excel with template and XML imported file by command line
Hi, I would like to use Excel 2003 to show reports to the customer. I have an Excel template (.xlt) for the presentation and a XML file containing the data with also the XSD inline for the schema description. How could I open Excel with the template *and* importing a XML file from the command line (to be open from a Windows shortcut) ? In the UI that's done manually by opening the template, then importing the XML by selecting the Data menu > XML, and Import XML. The command line options seems very rudimentary. Is there a way to do that with a script or a macro, knowing that there...

Entering Data #2
I have a large complex spreadsheet that calculates just when the file is saved. I was wondering if anyone new how to ensure that the same number is not entered twice into a column other than using the find command. Ryabn -- RJ31 ------------------------------------------------------------------------ RJ31's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=30451 View this thread: http://www.excelforum.com/showthread.php?threadid=501138 Here are 2 formula's that will both work (Array formulas so commit with CTRL+SHIFT+ENTER): 1) =SUMPRODUCT(IF(COUNTIF(A1:A...

Sum data in workbook 1 and write result in workbook 2
Hi, I need some help with excel macro! I have two workbooks. Now I need to sum all cells with some date criteria from first workook and result must be inserted in cell in second workbook (for example in cell A1) WorkBook1 data example Column B (dates) Column C (payroll) 12.05.2005 1200 12.05.2005 5000 14.05.2005 3100 14.05.2005 8800 17.05.2005 3550 .... For example: sum all values in column C where date in column B is 12.05.2005 and put result in workbook 2 in cell A1 Thanks for help. Martin You don'...

File with packed-data
How do I convert a file containing packed-data to a file in ASCII-text format? For example: From: ....c.............00000000.. ........ 00087202320000000FFFFFFFF20408000103 00232C087C20C000C000000007202C00072C To: 0000283722 0028372 02000 0000000 270240028 000000017023 I don't think all your fields are packed. FFFFFFFF 00000000 Doesn't looked packed to me. And I think that a lot of FTP programs will "fix" some of the funny packed decimals when you transfer them to the pc (from a mainframe???). (I think they try to translate some rudimentary graphic characters to pc...

Retaining Daily Data Automatically
Hi Ive got a spreadsheet which takes data from tab 1 (which is a list sequential list of dates in column A followed by the data in B) & updates a cell in tab 2 with the relevant data when TODAY matches a date in the tab 1 master. I can get it to do this for one day, then it obviously overwrites the figures the next day. How do i keep it feeding the info daily but keep a record of each days results in tab 2? Any help gratefully recieved cheers ...

How to Keep Money 2006 Data when Install Money 2005
I have downloaded a trial version of Money 2006 from the internet. I am happy enough to buy a full version of Money - but the website says that the full version of the Money 2006 download is only available in the USA. In the UK I have to buy Money 2005. Can I load my Money 2006 data into Money 2005? Thanks in advance for any advice. Peter No. Money is not backwards compatible and moreover you cannot convert between regions. USA > UK will not work. Money is heavily regionalised to take account of fiscal regulations etc. -- Regards Bob Peel, Microsoft MVP - Money For UK tips &...

User Data
Is it possible to access the user table in MSCRM? I want to capture some more details for given users say his/her competency level. I am not able to add a field via Deployment Manager. Can we do it using a custom build application? Competency has to be stored for each user. So I would require having competency level corresponding to each user. Hi, Modifying the fields for userinformation isn't possible in this version. In mscrm 3.0 it will be possible. You will have to wait untill the crm 3.0 release or built your own application. You can use the systemuserid for making the ma...

Xml digital Signature
Hi, I've a question about the xml digital signature procedure. I got a code written in C# that sign and verify an xml document and it works well, but the question is: How can I know that the signature come from a certain user? I know that is possible add the tags: <KeyInfo> <X509Data> <X509IssuerSerial> <X509IssuerName>CN=TAMURA Kent, OU=TRL, O=IBM, L=Yamato-shi, ST=Kanagawa, C=JP </X509IssuerName> <X509SerialNumber>12345678</X509SerialNumber> </X509IssuerSerial> ...

Simple data form
Hi, I'm creating a Shipping template in Excel, and I want a simple form to pop up for the line items. I can see how to do it using Data/Form, however is there another way to do it, by not making the user do that? Maybe so the form opens up when they get on the first cell of that part of the worksheet? Also I noticed when I do use this form, when I close it, it doesn't bring me back to the worksheet, but rather to the VB code. I don't want this to happen to my users. Any ideas? Thanks! Tracey:mad: ------------------------------------------------ ~~ Message posted from http:/...

XML file into Excel 2000
Hi, I have a xml document that i want Excel 2000 to open. I'm having difficulties reading the file in, is there a utility/parser built in to this version of excel?....or will i need to use a 3rd party tool? thanks. I vaguely remember something about an XML parser being available for download on the MicroSoft website, but other than that I don't know. -- Ian -- <vinojairath@gmail.com> wrote in message news:1157450278.182285.80190@d34g2000cwd.googlegroups.com... > > Hi, > > I have a xml document that i want Excel 2000 to open. I'm having > difficulties...

Replace data in a column
Okay, I have a column that has text in it. I want to change all these to the Identifying number that is found in a different table. So how do I do this? Example: Current table has: A 1 First name 2 New first name 3 Another first name Need to change the A column in first table to the correspoding column in this table: A B 1 12 First name 2 15 New first name 3 22 Another first name So after the switch either I need a new column in the current table so it look like the second column (there might be more then one match in the current t...

Ignore Blank/Data Validation
Can anyone tell me how that feature is suppose to work? If you uncheck the "Ignore Blank" option when creating data validation, I would assume it would alert the user if they left that cell blank. This doesn't seem to do that. When does "Ignore Blank" get triggered? I understand how Access works when making a field required but Excel doesn't have the same event driven programming. Thanks and Happy Holiday! LeAnn Data Validation can't be used to force an entry. A user can always select a cell and move on without making an entry. The Ignore Blank opti...

Totalling data from several workbooks to a Summary Sheet
I am trying (unsuccessfully) in creating a formula, that will allow me to total data from several workbooks, to a Summary sheet within Excel. Does anyone know how to complete this? A guess, although may not do what you want...? Maybe gives you a hint depending on your ultimate requirement. =SUM(Sheet2!F1:F5,Sheet3!G1:G5) HTH regards, Howard "Phillip Anderson" <PhillipAnderson@discussions.microsoft.com> wrote in message news:F1AC386F-B890-4132-BD26-CA3BBE158FC4@microsoft.com... >I am trying (unsuccessfully) in creating a formula, that will allow me to ...

Reporting Services And Object Data Source 06-18-10
Hi, I've a report with a object data source. I've a entity (A) with a property (b) that return an object (C) with other property (D). how can i put a property D in my report?" Thank in advance Giorgio Hi Giorgio, To view the property D in your report data source, you need to create your bussiness objects like this: 1. Class C class BusinessObjectC { private string d; public BusinessObjectC(string strD) { d = strD; } public string D { get { return d; } } } 2. Class A ...

Where can we get xml scheme for tuning setting of WM2003/WM5.0 PDA
Hi all, We are working upon setting up a lot of WM2003/WM5.0 empowered mobile devices according with company standards. As far as I know these settings are tuned through xml file. Then this xml file has to be deployed deployed/executed onto mobile device. My question is where xml scheme for tuning such setting is published so we may assign necessary properties referencing it? Thank you in advance. ...

Excel2002: Data > Sort ruins links! Any suggestions?
Hi Problem: if I do a sort on one page (ie worksheet) then all the other pages taking info from that page will now point to the wrong cells! This problem happens both if I use ordinary addressees and also if I use defined "names" instead! Any suggestions? Ship Think you'll need to use for e.g. VLOOKUP's or OFFSET(..MATCH()...) in the dependent sheets rather than just simple links to cells in the "master" sheet This arrangement will work if you have a key column that is shared between the dependent sheets and the "master" sheet with no duplicates i...

graph won't update when data is changed
I've tried everything imaginable and can't get charts to update when I edit data. This was never a problem in Office 2003. The problem seems to be related to how Excel 2007 saves files, since when I first create the charts everything is fine until I save the file (in xltm format) and then reopen it. I just doesn't seem like Office 2007 is ready for prime time - on did I miss the memo: Is this version still in Alpha? ...

Populate Fields
Hello, On my Access form have 2 fields. Code and Code Description. The code field does a look up on a table and allows the user to select the code they want. The table contains the code and code description fields only. What I'd like to be able to do, is if a user selects code A1 (for example) the code description field automatically populates with the code description. How can I achieve this? Thanks all! Is the control for the code field a combo box? If so, you can change the row source to include the description in an additional column as long as you can construct the query pr...

need to populate several cells based on drop down menu selection
I have created a validation drop down menu for specific options and I am now trying to automatically populate different cells in this worksheet based on the option chosen. You can use a VLookup formula to return the matching values. There's an example here: http://www.contextures.com/xlOrderForm01.html TerriF wrote: > I have created a validation drop down menu for specific options and I am now > trying to automatically populate different cells in this worksheet based on > the option chosen. -- Debra Dalgleish Excel FAQ, Tips & Book List http://www.contextures.com...

How to read in a binary formatted file and parse the contents and populate a listview window?
Just as the subject states...Pretty new to this stuff... ReadFile works. File::Read works. fread works. Define "parse". Any introductory book on C programming, including the venerable K&R book, tells how to do this. Your problem statement is far too vague to make it possible to give a concrete example. Also, don't put messages in the headers and then refer to the header. Most newsreaders truncate the amount of the header that is visible. joe On 29 Jun 2006 13:02:46 -0700, Perry.Moa@gmail.com wrote: >Just as the subject states...Pretty new to this stuff... J...

getting data from one sheet to another
I am working on a labor chart. The first sheet has the names and day of the week. I have them formatted to just put in the number of hours and it figures pay. There are 4 different job descriptions and at the bottom of each day, it totals the dollars spent for labor on that day. My second sheet has sales and will divide the cost of labor by sales and give me a percentage. On the second page, how can I get the total dollars in labor for each catagory from sheet one to a certain cell on sheet 2 without manually entering it. I know it can be done as I have seen it before Assuming the da...

data in a dynamically linked dll
I need to use a dll that is dynamically linked to a program (sometimes it exists, and sometimes not). The dll exports some functions and some data (integers). I can easily get at the functions with GetProcAddress, but how do I get at the data? Building the program gives me unresolved external references errors. If DLL is mine, then I can add functions that will pass appropriate data, but what if I cannot do that? What am I missing? Thanks Henryk Birecki Use accesser functions to get the data. AliR. "Henryk Birecki" <soaringpilot@sbcglobal.net> wrote in message news:cpsjc3...

Problem Importing Fixed Width Data
I have a data set of about 700 entries from an old Pascal program that was exported to a text file in this fomat: ID # : 10106 Sex : Female Raw Scores : 1 4 1 0 3 6 3 T Scores : 53 114 65 43 64 83 42 I need the headers to be ID#, Sex, Raw Scores 1-7 and T Scores 1-7 and then to pick up the data and place it in the appropriate cell. Since the format is inverted, Excel cannot import is correctly. Is there a script that will accomplish this? Thank you for your help!! What column is the data in? Can the number of sets vary or is it always 700? What is the first row of ...

Data from another worksheet
Good Morning All, Using Windows and Excel XP. On sheet "A" I want to show data from sheets "B", "C" and "D". On sheets B,C and D the data is in column A but changes rows everyday. I know the formula on sheet "A" to show data on sheet "B" that is in a fixed location, e.g. =B!A1. How would I set up the formula that would look for the last row of data in column A on sheet "B". Thanks for any help. Michael One way is to try and get a match on a number larger than would be possible in your case =match(99999999...

? Virtual List Control Data Source
Hi, I've seen several examples and tutorials on using virtual list controls, but they all generated the text in one of the same three ways in the LVN_GETDISPINFO handlers: index some hypothetical array, convert a random number to a string, or convert the item/subitem number to a string. These are not very useful since I have always used the list control as the container for the data itself. I know that some people use external data sources even with non-virtual lists, but I have not needed to do that yet; writing and reading from the control has been sufficient for the sizes of data (tex...