Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to append data in xml

$
0
0

I have below script which will create xml with data.

======================================================================

# Set the File Name
$filePath = "e:\Report2.xml"
 
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$Null)
 
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
 
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
 
# Set the XSL
$XSLPropText = "type='text/xsl' href='style.xsl'"
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", $XSLPropText)
 
# Write Root Element
$xmlWriter.WriteStartElement("Execution")
 
# Write the Document
$xmlWriter.WriteStartElement("ExecutionStarted")
$xmlWriter.WriteAttributeString("Date",$date)
$xmlWriter.WriteStartElement("Environments1")
$xmlWriter.WriteStartElement("Colors")
$xmlWriter.WriteAttributeString("Red","21")
$xmlWriter.WriteAttributeString("Yellow","14")
$xmlWriter.WriteAttributeString("Green","18")
$xmlWriter.WriteEndElement()# Closing Colors
$xmlWriter.WriteEndElement() # Closing environments1


$xmlWriter.WriteStartElement("Environments2")
$xmlWriter.WriteStartElement("Names")
$xmlWriter.WriteAttributeString("John","21")
$xmlWriter.WriteAttributeString("Mike","14")
$xmlWriter.WriteAttributeString("Alex","18")
$xmlWriter.WriteEndElement()# Closing Name
$xmlWriter.WriteEndElement() # Closing environments2


$xmlWriter.WriteEndElement() # Closing executionstarted
# Write Close Tag for Root Element
$xmlWriter.WriteEndElement() # Closing RootElement
 
# End the XML Document
$xmlWriter.WriteEndDocument()
 
# Finish The Document
$xmlWriter.Finalize
$xmlWriter.Flush()
$xmlWriter.Close()

======================================================================

here is the xml data

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<Execution>
    <ExecutionStarted Date="">
        <Environments1>
            <Colors Red="21" Yellow="14" Green="18" />
        </Environments1>
        <Environments2>
            <Names John="21" Mike="14" Alex="18" />
        </Environments2>
    </ExecutionStarted>
</Execution>

==================================================================

I want to append new data e.g "Environments3" shown below. what will be the code for this?

<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='style.xsl'?>
<Execution>
    <ExecutionStarted Date="1">
        <Environments1>
            <Colors Red="21" Yellow="14" Green="18" />
        </Environments1>
        <Environments2>
            <Names John="21" Mike="14" Alex="18" />
        </Environments2>
    </ExecutionStarted>
   <ExecutionStarted Date="2">
        <Environments3>
            <Names blue="21" black="14" cyan="18" />
        </Environments3>
   </ExecutionStarted>
</Execution>


Thanks,



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>