我正在尝试将 XML 验证添加到现有的 Powershell 脚本中。我已经用 C# 编写代码作为概念证明,但是当我移植到 Powershell 并使用相同的输入文件时,我得到了不同的结果。我正在使用 Powershell 3.0。
我正在使用的 C# 代码是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
namespace SchemaTest
{
class Program
{
const string SCHEMA_PATH = @"C:\Test\test_schema.xsd";
const string XML_PATH = @"C:\Test\test.xml";
static void Main(string[] args)
{
XmlReader schemaReader = XmlReader.Create(SCHEMA_PATH);
XmlSchema schema = XmlSchema.Read(
schemaReader,
(Object sender, ValidationEventArgs e) =>
{
Console.Out.WriteLine("Error reading schema file");
}
);
schemaReader.Close();
XmlReaderSettings rSettings = new XmlReaderSettings();
rSettings.ValidationType = ValidationType.Schema;
rSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
rSettings.Schemas.Add(schema);
rSettings.ValidationEventHandler += (Object sender, ValidationEventArgs e) =>
{
Console.Out.WriteLine("Error validating XML file");
};
XmlReader configReader = XmlReader.Create(XML_PATH, rSettings);
XmlDocument doc = new XmlDocument();
doc.Load(configReader);
configReader.Close();
Console.ReadKey(true);
}
}
}
我有一个简单的 XSD 架构和相应的(无效的)XML 文件,当使用此代码运行时,它会从 rSettings.ValidationEventHandler
生成预期的“错误验证 XML 文件”消息。
我写的对应的Powershell代码如下:
$schemaReader = [System.Xml.XmlReader]::Create('C:\Test\test_schema.xsd')
[System.Xml.Schema.ValidationEventHandler]$schemaValidationHandler = { Write-Output "Error reading schema file" }
$schema = [System.Xml.Schema.XmlSchema]::Read($schemaReader, $schemaValidationHandler)
$schemaReader.Close()
$rSettings = New-Object -Type System.Xml.XmlReaderSettings
$rSettings.ValidationType = [System.Xml.ValidationType]::Schema
$rSettings.ValidationFlags = $rSettings.ValidationFlags -bor [System.Xml.Schema.XmlSchemaValidationFlags]::ReportValidationWarnings
$rSettings.Schemas.Add($schema)
Register-ObjectEvent -InputObject $rSettings -EventName ValidationEventHandler -Action { Write-Output 'Error validating XML file' }
$configReader = [System.Xml.XmlReader]::Create('C:\Test\test.xml', $rSettings)
$doc = New-Object -TypeName System.Xml.XmlDocument
$doc.Load($configReader)
$configReader.Close()
当我针对同一个 XSD 和 XML 文件运行此代码时,我没有像预期的那样得到“错误验证 XML 文件”输出。
为了排除故障,我注释掉了 Register-ObjectEvent
调用,并验证了在调用 $doc.Load($configReader)
行时我遇到了异常.这使我认为注册事件处理程序存在问题,但我似乎找不到替代方法。
为了完整性,这里是我用来测试的 XSD 和 XML 文件:
test_schema.xsd:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.test.com"
xmlns="http://www.test.com"
elementFormDefault="qualified"
version="1.0.0">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" />
<xs:element name="age" type="xs:int" />
<xs:element name="city" type="xs:string" />
</xs:sequence>
<xs:attribute name="id" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:schema>
测试.xml:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root
xmlns="http://www.test.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
id="1">
<name>Joe</name>
<!-- Comment out <age> tags for testing
<age>20</age>
-->
<city>New York City</city>
</root>
最佳答案
在 Powershell 中,事件处理程序作为作业运行。因此,它们的管道结果不会像在主脚本中的管道中那样显示在屏幕上。使用 Receive-Job 可以获得正在执行的作业的管道结果。命令。
由于 Write-Output
具有将对象显示到默认外流的效果,因此只有在从主脚本调用时才会实际显示到屏幕。
这chapter来自 Powershell Cook 一书,很好地解释了 Powershell 事件和作业。
关于xml - Powershell XML 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21415682/