0

I have some file .sfu, I would like to check the sum and get some information to write to XML file. I try this way but still can not get the expected xml file.

$File = Get-ChildItem -Path .\*B01-A1* -Filter *.sfu

$Path = ".\SUM.xml"

# get an XMLTextWriter to create the XML
$XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)

# choose a pretty formatting:
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"

# write the header
$xmlWriter.WriteStartDocument()

# create root element
$XmlWriter.WriteComment('System Information')
$xmlWriter.WriteStartElement('Data Details')

Foreach ($f in $File)
{
    $GetHash = Get-FileHash $f -Algorithm SHA256
    $HASH = $GetHash.HASH
    $size = $f.Length
    # add three pieces of information:
    $xmlWriter.WriteElementString('Name',$f)
    $xmlWriter.WriteElementString('SHA256',$HASH)
    $xmlWriter.WriteElementString('Size',$size)



    $xmlWriter.WriteEndElement()

}
 $xmlWriter.WriteEndElement()
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()

$xmlWriter.Close()

My expected XML file is like this

<?xml version="1.0"?>
<!--System Information-->
<Lists>
    <Data Details>
        <Name>111-B01-A1.sfu</Name>
        <SHA256>4afdfefearfarafaa</SHA256>
        <Size>10234</Size>
    </Data Details>
    <Data Details>
        <Name>111-B21-A1.sfu</Name>
        <SHA256>4afdfefeardsgafaa</SHA256>
        <Size>10234</Size>
    </Data Details>
</Lists

Anyone can help please thank you

0

1 Answer 1

2

There were quite a few errors with your code. For instance, you need to create a System.Xml.XmlWriterSettings to prepare the pretty formatting and not use read-only or non-existant properties of the $XmlWriter object.

The $Path needs to be absolute instead of relative and also, an elementname in XML cannot contain spaces, so Data Details won't work.

Anyway, here's your code revised:

$Files = Get-ChildItem -Path .\*B01-A1* -Filter *.sfu -File
$Path  = "D:\SUM.xml"  # Use an Absolute path here !!

# choose a pretty formatting:
$XmlSettings = [System.Xml.XmlWriterSettings]::new()
$XmlSettings.Indent = $true
$XmlSettings.IndentChars = "`t"
# create the XmlWriter object using the output path and settings
$XmlWriter = [System.XML.XmlWriter]::Create($Path, $XmlSettings)

# write the header and XML Declaration
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")
$XmlWriter.WriteComment('System Information')

# create root element
$xmlWriter.WriteStartElement('Lists') 
    # loop thrugh the files
    foreach ($f in $Files) {
        $Hash = (Get-FileHash -Path $f.FullName -Algorithm SHA256).Hash
        # add a 'File' element for each file
        $XmlWriter.WriteStartElement('DataDetails')   # an elementname cannot contain spaces
            # add three pieces of information:
            $xmlWriter.WriteElementString('Name',$f.Name)
            $xmlWriter.WriteElementString('SHA256',$Hash)
            $xmlWriter.WriteElementString('Size',$f.Length)
        # close the 'File' element
        $xmlWriter.WriteEndElement()
    }
# close the root element
$xmlWriter.WriteEndElement()
# finalize the document:
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.