如何在PowerShell中使用循环编写XML文件?

如何在PowerShell中使用循环编写XML文件?,xml,powershell,loops,Xml,Powershell,Loops,我有一些文件.sfu,我想检查总和并获取一些信息以写入XML文件。 我尝试了这种方法,但仍然无法获得预期的xml文件 $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 p

我有一些文件.sfu,我想检查总和并获取一些信息以写入XML文件。 我尝试了这种方法,但仍然无法获得预期的xml文件

$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()
我期望的XML文件如下所示

<?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

111-B01-A1.sfu
4afdfefearfarafaa
10234
111-B21-A1.sfu
4AFDFefeardsgafa
10234

你的代码有很多错误。例如,您需要创建一个
System.Xml.XmlWriterSettings
来准备漂亮的格式,而不是使用
$XmlWriter
对象的只读或不存在的属性

$Path
必须是绝对的,而不是相对的,而且XML中的elementname不能包含空格,因此
数据详细信息
不起作用

无论如何,下面是您的代码修订版:

$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()

希望对您有所帮助

您的代码中有不少错误。例如,您需要创建一个
System.Xml.XmlWriterSettings
来准备漂亮的格式,而不是使用
$XmlWriter
对象的只读或不存在的属性

$Path
必须是绝对的,而不是相对的,而且XML中的elementname不能包含空格,因此
数据详细信息
不起作用

无论如何,下面是您的代码修订版:

$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()

希望对你有所帮助

你能提供你意想不到的结果吗?你能提供你意想不到的结果吗?哇。。令人惊叹的!谢谢你,伙计。。尊敬@提奥瓦。。令人惊叹的!谢谢你,伙计。。尊敬@提奥