turranx
-
Total Posts
:
59
- Scores: 0
-
Reward points
:
0
- Joined: 2/7/2006
- Location: Cincinnati, OH
-
Status: offline
|
HowTo: Writing XML Files
Tuesday, July 15, 2008 2:38 AM
( permalink)
There's not a lot of comments in this example. Instead, there is a decent amount of clean code that shows you how it's done. Also consider reading http://www.w3schools.com/xml/default.asp cls
[xml] $xmlLog = '<?xml version="1.0" encoding="ISO-8859-1"?> <Bakery_Inventory><x /></Bakery_Inventory>';
# Generate Run Details
$xmlRunDetails = $xmlLog.CreateElement("Run_Details");
$xmlTimeStamp = $xmlLog.CreateElement("TimeStamp");
($xmlTimeStamp.AppendChild(($($xmlLog.CreateElement("Year"))))).psBase.InnerText = Get-Date -UFormat %Y;
($xmlTimeStamp.AppendChild(($($xmlLog.CreateElement("Month"))))).psBase.InnerText = Get-Date -Format %M;
($xmlTimeStamp.AppendChild(($($xmlLog.CreateElement("Day"))))).psBase.InnerText = Get-Date -Format %d;
($xmlTimeStamp.AppendChild(($($xmlLog.CreateElement("Time"))))).psBase.InnerText = Get-Date -UFormat %T;
$Catch_Output = $xmlRunDetails.AppendChild($xmlTimeStamp); # Catching the output seems necessary to keep PowerShell from dumping information to the screen
$Catch_Output = $xmlLog.Bakery_Inventory.AppendChild($xmlRunDetails);
$xmlCookie = $xmlLog.CreateElement("Cookie");
($xmlCookie.AppendChild(($($xmlLog.CreateElement("Name"))))).psBase.InnerText = "Chocolate Chip";
($xmlCookie.AppendChild(($($xmlLog.CreateElement("Path"))))).psBase.InnerText = "Assembly Line 1";
$intCounter = 1;
$xmlSample = $xmlLog.CreateElement("Random_Sample");
$xmlSample.SetAttribute("ID",[string]$intCounter);
($xmlSample.AppendChild(($($xmlLog.CreateElement("Oven_Name"))))).psBase.InnerText = "Bloodgett_1";
($xmlSample.AppendChild(($($xmlLog.CreateElement("Chocolate_Chip_Count"))))).psBase.InnerText = $(New-Object System.Random).next(1,10000);
($xmlSample.AppendChild(($($xmlLog.CreateElement("Diameter"))))).psBase.InnerText = $(New-Object System.Random).next(1,10000000);
$Catch_Output = $xmlCookie.AppendChild($xmlSample);
$intCounter += 1;
$xmlSample = $xmlLog.CreateElement("Random_Sample");
$xmlSample.SetAttribute("ID",[string]$intCounter);
($xmlSample.AppendChild(($($xmlLog.CreateElement("Oven_Name"))))).psBase.InnerText = "Bloodgett_1";
($xmlSample.AppendChild(($($xmlLog.CreateElement("Chocolate_Chip_Count"))))).psBase.InnerText = $(New-Object System.Random).next(1,10000);
($xmlSample.AppendChild(($($xmlLog.CreateElement("Diameter"))))).psBase.InnerText = $(New-Object System.Random).next(1,10000000);
$Catch_Output = $xmlCookie.AppendChild($xmlSample);
$Catch_Output = $xmlLog.Bakery_Inventory.AppendChild($xmlCookie);
$xmlLog.Save((join-path -path $(get-location).tostring() xmlBakery.txt));
Get-Content (join-path -path $(get-location).tostring() xmlBakery.txt); The output is this: <?xml version="1.0" encoding="ISO-8859-1"?>
<Bakery_Inventory>
<x />
<Run_Details>
<TimeStamp>
<Year>2008</Year>
<Month>7</Month>
<Day>15</Day>
<Time>10:35:58</Time>
</TimeStamp>
</Run_Details>
<Cookie>
<Name>Chocolate Chip</Name>
<Path>Assembly Line 1</Path>
<Random_Sample ID="1">
<Oven_Name>Bloodgett_1</Oven_Name>
<Chocolate_Chip_Count>4810</Chocolate_Chip_Count>
<Diameter>4810210</Diameter>
</Random_Sample>
<Random_Sample ID="2">
<Oven_Name>Bloodgett_1</Oven_Name>
<Chocolate_Chip_Count>3174</Chocolate_Chip_Count>
<Diameter>3174007</Diameter>
</Random_Sample>
</Cookie>
</Bakery_Inventory>
|
|
|
|