Xml .Net5.0 中的内联验证未找到 xsd
Xml inline valdiation in .Net5.0 does not find xsd
我想根据 XSD 文件验证一些 XML 文件。
我被迫将框架从 .Net4.7.2 更改为 .Net5.0。
这是编写和验证 XML 文件的代码:
namespace XmlValidation
{
class Programm
{
static void Main()
{
string file = "test.xml";
using (var writer = new StreamWriter(file))
using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, IndentChars = " " }))
{
new XmlSerializer(typeof(MyClass)).Serialize(xmlWriter, new MyClass() { Property1 = "Content" });
}
List<string> messages = new();
XmlReaderSettings xmlReaderSettings = new()
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
| XmlSchemaValidationFlags.ProcessSchemaLocation
| XmlSchemaValidationFlags.ReportValidationWarnings
};
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((sender, args) =>
{
Console.WriteLine(args.Message);
});
using (XmlReader reader = XmlReader.Create(file, xmlReaderSettings))
{
while (reader.Read()) ;
}
}
}
public class MyClass
{
[XmlAttribute("noNamespaceSchemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string xmlShemaFile = "schema0.xsd";
public string Property1 { get; set; }
public string Property2 { get; set; }
}
}
我得到 xml:
<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="schema0.xsd">
<Property1>Content</Property1>
</MyClass>
并且 xsd 与 test.xml 在同一目录中:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyClass" type="MyClass" />
<xs:complexType name="MyClass">
<xs:all>
<xs:element minOccurs="1" maxOccurs="1" name="Property1" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Property2" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
结果是:
"Could not find schema information for the element 'MyClass'."
"Could not find schema information for the element 'Property1'."
同样适用于 .NetFramework 4.7.2。
"Der Inhalt des Elements 'MyClass' ist unvollständig. Erwartet wurde die Liste der möglichen Elemente: 'Property2'."
为什么这不适用于 .Net5.0?
正如评论中提到的 Mickel Kay 和 Martin Honnen,添加一个 XmlResolver 就成功了:
XmlReaderSettings xmlReaderSettings = new()
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
| XmlSchemaValidationFlags.ProcessSchemaLocation
| XmlSchemaValidationFlags.ReportValidationWarnings,
XmlResolver = new XmlUrlResolver(),
};
我想根据 XSD 文件验证一些 XML 文件。
我被迫将框架从 .Net4.7.2 更改为 .Net5.0。
这是编写和验证 XML 文件的代码:
namespace XmlValidation
{
class Programm
{
static void Main()
{
string file = "test.xml";
using (var writer = new StreamWriter(file))
using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = true, IndentChars = " " }))
{
new XmlSerializer(typeof(MyClass)).Serialize(xmlWriter, new MyClass() { Property1 = "Content" });
}
List<string> messages = new();
XmlReaderSettings xmlReaderSettings = new()
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
| XmlSchemaValidationFlags.ProcessSchemaLocation
| XmlSchemaValidationFlags.ReportValidationWarnings
};
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((sender, args) =>
{
Console.WriteLine(args.Message);
});
using (XmlReader reader = XmlReader.Create(file, xmlReaderSettings))
{
while (reader.Read()) ;
}
}
}
public class MyClass
{
[XmlAttribute("noNamespaceSchemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string xmlShemaFile = "schema0.xsd";
public string Property1 { get; set; }
public string Property2 { get; set; }
}
}
我得到 xml:
<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="schema0.xsd">
<Property1>Content</Property1>
</MyClass>
并且 xsd 与 test.xml 在同一目录中:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyClass" type="MyClass" />
<xs:complexType name="MyClass">
<xs:all>
<xs:element minOccurs="1" maxOccurs="1" name="Property1" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Property2" type="xs:string" />
</xs:all>
</xs:complexType>
</xs:schema>
结果是:
"Could not find schema information for the element 'MyClass'."
"Could not find schema information for the element 'Property1'."
同样适用于 .NetFramework 4.7.2。
"Der Inhalt des Elements 'MyClass' ist unvollständig. Erwartet wurde die Liste der möglichen Elemente: 'Property2'."
为什么这不适用于 .Net5.0?
正如评论中提到的 Mickel Kay 和 Martin Honnen,添加一个 XmlResolver 就成功了:
XmlReaderSettings xmlReaderSettings = new()
{
ValidationType = ValidationType.Schema,
ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema
| XmlSchemaValidationFlags.ProcessSchemaLocation
| XmlSchemaValidationFlags.ReportValidationWarnings,
XmlResolver = new XmlUrlResolver(),
};