.NET8 中的 XSD 工具 – 第 2 部分 – C# 验证
.NET8 环境中可用的 XSD 工具实用指南。
1 在 .NET8 中进行 XML 和 XSD 相关工作
我最近在 .NET8 环境中做一些与 XML 和 XSD 处理相关的工作,并创建了几个 概念验证 应用程序来评估可用的工具。 这些文章是我 原型工作 的结果。
1.1 使用/测试的工具列表
以下是使用/测试的工具
- Visual Studio 2022
- XSD.EXE(微软许可,VS2022 的一部分)
- XmlSchemaClassGenerator (开源/免费软件)
- LinqToXsdCore (开源/免费软件)
- Liquid XML Objects (商业许可)
1.2 本系列文章
由于技术原因,我将把这篇文章组织成几篇文章
- .NET8 中的 XSD 工具 – 第 1 部分 – VS2022
- .NET8 中的 XSD 工具 – 第 2 部分 – C# 验证
- .NET8 中的 XSD 工具 – 第 3 部分 – XsdExe – 简单
- .NET8 中的 XSD 工具 – 第 4 部分 – XsdExe - 高级
- .NET8 中的 XSD 工具 – 第 5 部分 – XmlSchemaClassGenerator – 简单
- .NET8 中的 XSD 工具 – 第 6 部分 – XmlSchemaClassGenerator – 高级
- .NET8 中的 XSD 工具 – 第 7 部分 – LinqToXsdCore – 简单
- .NET8 中的 XSD 工具 – 第 8 部分 – LinqToXsdCore – 高级
- .NET8 中的 XSD 工具 – 第 9 部分 – LiquidXMLObjects – 简单
- .NET8 中的 XSD 工具 – 第 10 部分 – LiquidXMLObjects – 高级
2 XML 和 XSD 示例
我为测试目的创建的一些示例 XML 和 XSD。
2.1 简单案例
<?xml version="1.0" encoding="utf-8"?>
<!--SmallCompany.xsd++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="https://markpelf.com/SmallCompany.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="SmallCompany">
<xs:complexType>
<xs:sequence>
<xs:element name="CompanyName" type="xs:string" />
<xs:element maxOccurs="unbounded" name="Employee">
<xs:complexType>
<xs:sequence>
<!--Name_String_NO is String NotOptional-->
<xs:element name="Name_String_NO" type="xs:string" />
<!--City_String_O is String Optional-->
<xs:element minOccurs="0" name="City_String_O" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="InfoData">
<xs:complexType>
<xs:sequence>
<!--Id_Int_NO is Int NotOptional-->
<xs:element name="Id_Int_NO" type="xs:int" />
<!--Quantity_Int_O is Int Optional-->
<xs:element minOccurs="0" name="Quantity_Int_O" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
有效的 XML
<?xml version="1.0" encoding="utf-8"?>
<!--SmallCompanyAAA.xml+++++++++++++++++++++++++++++++++++++++++++++++-->
<SmallCompany xmlns="https://markpelf.com/SmallCompany.xsd">
<CompanyName>SmallCompanyAAA</CompanyName>
<Employee>
<Name_String_NO>Mark</Name_String_NO>
<City_String_O>Belgrade</City_String_O>
</Employee>
<Employee>
<Name_String_NO>John</Name_String_NO>
</Employee>
<InfoData>
<Id_Int_NO>11</Id_Int_NO>
<Quantity_Int_O>123</Quantity_Int_O>
</InfoData>
<InfoData>
<Id_Int_NO>22</Id_Int_NO>
</InfoData>
</SmallCompany>
无效的 XML
<?xml version="1.0" encoding="utf-8"?>
<!--SmallCompanyAAA.xml+++++++++++++++++++++++++++++++++++++++++++++++-->
<SmallCompany xmlns="https://markpelf.com/SmallCompany.xsd">
<CompanyName>SmallCompanyAAA</CompanyName>
<Employee>
<Name_String_NO>Mark</Name_String_NO>
<City_String_O>Belgrade</City_String_O>
</Employee>
<Employee>
<Name_String_NO>John</Name_String_NO>
</Employee>
<InfoData>
<Id_Int_NO>11</Id_Int_NO>
<Quantity_Int_O>123</Quantity_Int_O>
</InfoData>
<InfoData>
<Id_Int_NO>22</Id_Int_NO>
</InfoData>
<Offending_Element>
Bla_Bla
</Offending_Element>
</SmallCompany>
2.2 高级案例
与上一篇文章相同。
3 C# 中的验证
编写 C# 代码来“为某些 XSD 验证 XML”非常基础,但仍然是正确的起点。
3.1 C# 代码
这是在 C# 中为某些 XSD 进行 XML 验证的示例代码。
public static bool ValidateXmlForXsd(
Microsoft.Extensions.Logging.ILogger? _logger,
string xmlPath, string xsdPath)
{
bool isValid=false;
XmlDocument xml = new XmlDocument();
xml.Load(xmlPath);
xml.Schemas.Add(null, xsdPath);
try
{
xml.Validate(null);
isValid = true;
}
catch (XmlSchemaValidationException ex)
{
string text1 = $"Validation FAILED for: xmlPath={xmlPath}" +
$" and xsdPath={xsdPath} .Validation Error: " +
ex.Message;
_logger?.LogInformation(text1);
isValid = false;
}
if (isValid)
{
string text1 = $"Validation SUCCESS for: xmlPath={xmlPath}" +
$" and xsdPath={xsdPath}";
_logger?.LogInformation(text1);
}
return isValid;
}
3.2 成功案例日志
这是验证成功案例的日志。
XsdExample01-Start------------
xmlFile11_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompanyAAA.xml
xsdFile1_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompany.xsd
xmlFile21_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompanyMMM.xml
xsdFile2_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompany.xsd
Validation SUCCESS for:
xmlPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompanyAAA.xml
and xsdPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompany.xsd
Validation SUCCESS for:
xmlPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompanyMMM.xml
and xsdPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompany.xsd
XsdExample01-End------------
3.3 失败案例日志
这是验证失败案例的日志。
XsdExample01-Start------------ xmlFile11_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompanyAAA.xml xsdFile1_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompany.xsd xmlFile21_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompanyMMM.xml xsdFile2_FullPath: C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompany.xsd Validation FAILED for: xmlPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompanyAAA.xml and xsdPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\SmallCompany.xsd . Validation Error: The element 'SmallCompany' in namespace 'https://markpelf.com/SmallCompany.xsd' has invalid child element 'Offending_Element' in namespace 'https://markpelf.com/SmallCompany.xsd'. List of possible elements expected: 'InfoData' in namespace 'https://markpelf.com/SmallCompany.xsd'. Validation SUCCESS for: xmlPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompanyMMM.xml and xsdPath=C:\TmpXSD\XsdExample_Validate\Example01\bin\Debug\net8.0\XmlFiles\BigCompany.xsd XsdExample01-End------------
4 参考
[1] XML 架构
https://w3schools.org.cn/xml/xml_schema.asp
[21] .NET8 中的 XSD 工具 – 第 1 部分 – VS2022
https://codeproject.org.cn/Articles/5388391/XSD-Tools-in-NET8-Part1-VS2022
[22] .NET8 中的 XSD 工具 – 第 2 部分 – C# 验证
https://codeproject.org.cn/Articles/5388393/XSD-Tools-in-NET8-Part2-Csharp-validation
[23] .NET8 中的 XSD 工具 – 第 3 部分 – XsdExe – 简单
https://codeproject.org.cn/Articles/5388396/XSD-Tools-in-NET8-Part3-XsdExe-Simple
[24] .NET8 中的 XSD 工具 – 第 4 部分 – XsdExe - 高级
https://codeproject.org.cn/Articles/5388483/XSD-Tools-in-NET8-Part4-XsdExe-Advanced
[25] .NET8 中的 XSD 工具 – 第 5 部分 – XmlSchemaClassGenerator – 简单
https://codeproject.org.cn/Articles/5388548/XSD-Tools-in-NET8-Part5-XmlSchemaClassGenerator-Si
[26] .NET8 中的 XSD 工具 – 第 6 部分 – XmlSchemaClassGenerator – 高级
https://codeproject.org.cn/Articles/5388549/XSD-Tools-in-NET8-Part6-XmlSchemaClassGenerator-Ad
[27] .NET8 中的 XSD 工具 – 第 7 部分 – LinqToXsdCore – 简单
https://codeproject.org.cn/Articles/5388628/XSD-Tools-in-NET8-Part7-LinqToXsdCore-Simple
[28] .NET8 中的 XSD 工具 – 第 8 部分 – LinqToXsdCore – 高级
https://codeproject.org.cn/Articles/5388629/XSD-Tools-in-NET8-Part8-LinqToXsdCore-Advanced
[29] .NET8 中的 XSD 工具 – 第 9 部分 – LiquidXMLObjects – 简单
https://codeproject.org.cn/Articles/5388683/XSD-Tools-in-NET8-Part9-LiquidXMLObjects-Simple
[30] .NET8 中的 XSD 工具 – 第 10 部分 – LiquidXMLObjects – 高级
https://codeproject.org.cn/Articles/5388684/XSD-Tools-in-NET8-Part10-LiquidXMLObjects-Advanced