为什么我使用System.Xml.XmlReader的代码检测到无效的XML文件?

问题描述 投票:1回答:1

目标

使用PowerShell 5.1,通过使用Microsoft的System.Xml.XmlReader对XML模式进行验证来检测无效的XML文件。我将通过捕获XMLException引发XML解析错误的XmlReader来检测无效的XML文件。

注意:我不想使用PowerShell社区扩展Test-Xml cmdlet。

问题

在解析无效的XML文件时,代码行$readerResult = $xmlReader.Read()不会抛出我期望的XMLException

参考

Validation Using the XmlSchemaSet

XmlReader Class

我的XSD

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:config-file-schema">
  <xs:element name="notes">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="note" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="to"/>
              <xs:element name="from">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute type="xs:byte" name="type" use="optional"/>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
              <xs:element type="xs:string" name="heading"/>
              <xs:element type="xs:string" name="body"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我的XML无效(第二行使用虚假元素名称notXXXes

<?xml version="1.0" encoding="UTF-8"?>
<notXXXes xmlns="urn:config-file-schema">
    <note>
        <to>Tove</to>
        <from type="1">Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
    </note>
    <note>
        <to>Bob</to>
        <from type="2">KeyW</from>
        <heading>Reminder</heading>
        <body>I won't</body>
    </note>
</notes>

我的代码

运行时,$readerResult返回true,表示下一个节点已成功读取。我希望$xmlReader.Read()抛出XMLException,因为XML文件内容违反了架构。

cls
$error.clear()

try
{

    [System.Xml.Schema.XmlSchemaSet] $schemaSet = New-Object -TypeName System.Xml.Schema.XmlSchemaSet
    $schemaSet.Add("urn:config-file-schema","C:\Users\x\Desktop\test.xsd");

    [System.Xml.XmlReaderSettings] $readerSettings = New-Object -TypeName System.Xml.XmlReaderSettings
    $readerSettings.Schemas = $schemaSet
    $readerSettings.ValidationType = [System.Xml.ValidationType]::Schema
    $readerSettings.ConformanceLevel = [System.Xml.ConformanceLevel]::Fragment
    $readerSettings.IgnoreWhitespace = $true;
    $readerSettings.IgnoreComments = $true;

    [System.Xml.XmlReader]$xmlReader = [System.Xml.XmlReader]::Create("C:\Users\x\Desktop\test.xml", $readerSettings);

    #just to show that Schemas was set up OK
    "target namespace: " + $readerSettings.Schemas.Schemas().TargetNamespace

    $readerResult = $xmlReader.Read()

    "readerResult: " + $readerResult
}
catch
{
    "error: " + $error
}
finally
{
    $xmlReader.Close()
}

编辑#1

该片段将从文件中读取每行XML并显示其元数据

while ($xmlReader.Read())
{
    write-console ("Depth:{0,1} Name:{1,-10} NodeType:{2,-15} Value:{3,-30}" -f $xmlReader.Depth, $xmlReader.Name, $xmlReader.NodeType, $xmlReader.Value)
}
xml xsd-validation xmlreader
1个回答
1
投票

XmlReader概念的重点是它是一种处理XML的流式方法。这允许您访问大型/复杂的XML文档,而无需将整个内容保存在内存中(并且,如果您使用DOM样式访问,则需要多层额外的内存使用来启动)。

这在内存使用方面是有效的,但确实意味着仅在遇到问题的节点时报告错误。

第一个Read在这里读取XML声明 - <?xml version="1.0" encoding="UTF-8"?> - 它看似很好,不应该引起任何错误。如果您需要验证整个文档,那么您需要完整地直到最后才能使用Read。但如果这是你唯一的目的,我可能会推迟到你要解雇的Test-Xml命令行开关。

© www.soinside.com 2019 - 2024. All rights reserved.