【问题标题】:How to write XML file with loop in PowerShell?如何在 PowerShell 中使用循环编写 XML 文件?
【发布时间】:2020-07-28 07:50:33
【问题描述】:

我有一些文件 .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

谁能帮忙,谢谢

【问题讨论】:

  • 能否请您也提供您意想不到的结果?

标签: xml powershell loops


【解决方案1】:

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

$Path 必须是绝对的而不是相对的,而且 XML 中的元素名不能包含空格,因此 Data Details 将不起作用。

不管怎样,这里是你的代码修改:

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

希望有帮助

【讨论】:

  • 哇.. 太棒了!谢谢你,伙计..尊重! @Theo
猜你喜欢
  • 2013-07-09
  • 2023-03-23
  • 1970-01-01
  • 2020-08-06
  • 2018-10-28
  • 2013-10-24
  • 1970-01-01
  • 2021-04-17
  • 1970-01-01
相关资源
最近更新 更多