I am trying to create an XML file with powershell. (My first project!) I have a directory c:\scripts\xmlimages that has the repository of all images that I want to add to my “base file” which is attached. (I am trying to add a bunch of emoticons to the corporate IM solution.) I started updating the 4 fields below manually. 1. yoga next to defaultkey, 2.yoga.gif next to image, 3. yoga next to text, 4. incrementing the number after order … The line looks like below, and a file is attached… you can see the repitition. After spending hours on this, I realized I could try to do this in powershell, but not really sure where to begin. More specifically, I expect to get many more requests down the road to add these emoticons, and so decided let’s figure this out. I can use my base file which currently has about 131 lines of different graphics, and then append the new file generated from the c:\scripts\xmlimages where I can place new images, and then output this to a new xml file. Would appreciate any and all feedback!
It’s been a long week - you may have to dumb this down for me a bit. I’m seeing the word “yoga” a lot, and thinking, “no, I’ll have whiskey.”
First, if the goal is to read in an existing XML file and add elements to it, that’s easy. “PowerShell in Depth” has a lovely chapter on the subject, in fact, chock full of examples. There’s also a million tutorials online: http://powershell.com/cs/blogs/tobias/archive/2009/02/02/xml-part-2-write-add-and-change-xml-data.aspx, https://www.simple-talk.com/sysadmin/powershell/powershell-data-basics-xml/, http://virtualengine.co.uk/2013/updating-and-writing-xml-files-with-powershell/, pretty much take your pick.
Second, it’s also possible to just treat the XML file as a giant text file. Read it in, discard the last line (which has the closing element), add the element text you want, add back the closing element, and dump the result to a new text file.
I don’t know how much you know about XML, but you can’t just append stuff to the end of one - each document needs a single top-level element, so new data has to go within those.
I hope that maybe helps a little. If you’ve got a specific question about how to do a particular task, I can try and narrow in.
thx for the quick response! I will check those resources
I guess this didn’t come through…
let’s try this again…
let’s try this again…apparently I don’t know how to post an xml line so I am removing certain characters:
emoticon defaultKey="(yoga)" image="yoga.gif" text="yoga" order="131"
XML is close enough to HTML to not work on a website. You can attach a .TXT file, though. And I saw your original XML example. That’s fine… if you run into a specific question on a task, happy to try and help.