不同类型的 XML 验证
Different types of XML validation
我正在寻找一些东西来验证 XML 我们是针对 XSD 发送的。我遇到过这三个,但似乎只有一个 'work' 我猜有一个标记和问题是其他人没有的原因,但想知道什么是最好的使用方法和区别,除了完成方式,用这三个。
XML
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Forename>John</Forename>
</Person>
XSD
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" version="0.2">
<xs:annotation>
<xs:documentation>
</xs:documentation>
</xs:annotation>
<xs:element name ="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Forename" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
<xs:element name="Middlename" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
第一个标记错误,姓氏元素是预期的,但不在我预期的 XML 中。
class XPathValidation
{
static void Main()
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(@"C:\test\test.xsd"));
XDocument doc = XDocument.Load(@"C:\test\test.xml");
Console.WriteLine("Validating doc1");
bool errors = false;
doc.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
});
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.ReadKey();
}
}
这两个都只是运行和return而已。
class XmlSchemaSetExample
{
static void Main()
{
XmlReaderSettings booksSettings = new XmlReaderSettings();
booksSettings.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"C:\test\test.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
XmlReader books = XmlReader.Create(@"C:\test\test.xml", booksSettings);
while (books.Read()) { }
Console.ReadKey();
}
static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
Console.Write("WARNING: ");
Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
Console.Write("ERROR: ");
Console.WriteLine(e.Message);
}
}
}
和
class XPathValidation
{
static void Main()
{
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"C:\test\test.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(@"C:\test\test.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
// the following call to Validate succeeds.
document.Validate(eventHandler);
// the document will now fail to successfully validate
document.Validate(eventHandler);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning {0}", e.Message);
break;
}
}
}
感谢您提供信息,仍在学习所有这些。
我认为后两个不起作用,因为当您将架构添加到 XmlReaderSettings
时,您为 targetNamespace
提供的值不正确。这应该是一个空字符串,因为您的 XML 没有命名空间(或 null
,如 per the docs,这将从架构中推断出命名空间)。
至于哪个更好,就看你的需求了。如果只是为了验证它,使用 XmlReader
的选项 2 是首选,因为它不会以将整个 XML 加载到 DOM 为代价,然后您会扔掉它。
如果您确实需要使用 DOM 查询 XML,XDocument
/ LINQ to XML API(选项 1)是API 比旧的 XmlDocument
API(选项 3)更好,更现代 API。
我正在寻找一些东西来验证 XML 我们是针对 XSD 发送的。我遇到过这三个,但似乎只有一个 'work' 我猜有一个标记和问题是其他人没有的原因,但想知道什么是最好的使用方法和区别,除了完成方式,用这三个。
XML
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Forename>John</Forename>
</Person>
XSD
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" version="0.2">
<xs:annotation>
<xs:documentation>
</xs:documentation>
</xs:annotation>
<xs:element name ="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="Forename" type="xs:string"/>
<xs:element name="Surname" type="xs:string"/>
<xs:element name="Middlename" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
第一个标记错误,姓氏元素是预期的,但不在我预期的 XML 中。
class XPathValidation
{
static void Main()
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(@"C:\test\test.xsd"));
XDocument doc = XDocument.Load(@"C:\test\test.xml");
Console.WriteLine("Validating doc1");
bool errors = false;
doc.Validate(schemas, (o, e) =>
{
Console.WriteLine("{0}", e.Message);
errors = true;
});
Console.WriteLine("doc1 {0}", errors ? "did not validate" : "validated");
Console.ReadKey();
}
}
这两个都只是运行和return而已。
class XmlSchemaSetExample
{
static void Main()
{
XmlReaderSettings booksSettings = new XmlReaderSettings();
booksSettings.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"C:\test\test.xsd");
booksSettings.ValidationType = ValidationType.Schema;
booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler);
XmlReader books = XmlReader.Create(@"C:\test\test.xml", booksSettings);
while (books.Read()) { }
Console.ReadKey();
}
static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
{
Console.Write("WARNING: ");
Console.WriteLine(e.Message);
}
else if (e.Severity == XmlSeverityType.Error)
{
Console.Write("ERROR: ");
Console.WriteLine(e.Message);
}
}
}
和
class XPathValidation
{
static void Main()
{
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.w3.org/2001/XMLSchema", @"C:\test\test.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create(@"C:\test\test.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler);
// the following call to Validate succeeds.
document.Validate(eventHandler);
// the document will now fail to successfully validate
document.Validate(eventHandler);
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning {0}", e.Message);
break;
}
}
}
感谢您提供信息,仍在学习所有这些。
我认为后两个不起作用,因为当您将架构添加到 XmlReaderSettings
时,您为 targetNamespace
提供的值不正确。这应该是一个空字符串,因为您的 XML 没有命名空间(或 null
,如 per the docs,这将从架构中推断出命名空间)。
至于哪个更好,就看你的需求了。如果只是为了验证它,使用 XmlReader
的选项 2 是首选,因为它不会以将整个 XML 加载到 DOM 为代价,然后您会扔掉它。
如果您确实需要使用 DOM 查询 XML,XDocument
/ LINQ to XML API(选项 1)是API 比旧的 XmlDocument
API(选项 3)更好,更现代 API。