C# XML Schema 验证

标签 c# xml validation xsd

我有一个像这样的不错的 XML 文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <Assets Path="C:\Users\r3plica\Google Drive">
        <Assetd>
            <FileName>Boomerang - Error codes.xlsx</FileName>
            <DisplayName>Boomerang - Error codes</DisplayName>
            <Description>This is the Boomerang error codes file</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Boomerang</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Assetd>
        <Asset>
            <FileName>Issue Tracker v5.xlsx</FileName>
            <Description>This is the issue tracker for Skipstone</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Skipstone</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
    </Assets>

然后我有我这样创建的模式:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="data"
        targetNamespace="http://tempuri.org/data.xsd"
        elementFormDefault="qualified"
        xmlns="http://tempuri.org/data.xsd"
        xmlns:mstns="http://tempuri.org/data.xsd"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
      <xs:element name="Assets">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Asset" type="Asset" minOccurs="1" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>

      <xs:complexType name="Asset">
        <xs:sequence>
          <xs:element name="FileName" type="xs:string" minOccurs="1" maxOccurs="1" />
          <xs:element name="DisplayName" type="xs:string" minOccurs="0" maxOccurs="1" />
          <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
          <xs:element name="Tags" type="Tags" minOccurs="0" maxOccurs="1" />
          <xs:element name="Categories" type="Categories" minOccurs="1" maxOccurs="1" />
        </xs:sequence>
      </xs:complexType>

      <xs:complexType name="Tags">
        <xs:sequence>
          <xs:element name="Tag" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:complexType>

      <xs:complexType name="Categories">
        <xs:sequence>
          <xs:element name="Category" type="xs:int" minOccurs="1" maxOccurs="unbounded" />
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

据我所知,xml 文件无效,因为第一个元素是 Asset 而不是 Asset,但是如果我运行我的 C# 代码:

XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("http://tempuri.org/data.xsd", "data.xsd");

XDocument doc = XDocument.Load(openFileDialog1.FileName);
string msg = "";
doc.Validate(schemas, (o, err) =>
{
    msg = err.Message;
});
Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg);

它告诉我 xml 是有效的... 如果我使用这段代码:

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("http://tempuri.org/data.xsd", "data.xsd");
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create(openFileDialog1.FileName, settings);

// Parse the file. 
while (reader.Read()) ;

我在控制台中得到这个输出:

Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Assets'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the attribute 'Path'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Assetd'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'DisplayName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Asset'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'FileName'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Description'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tags'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Tag'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Categories'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.
Warning: Matching schema not found.  No validation occurred. Could not find schema information for the element 'Category'.

谁能告诉我我做错了什么?快要死了:(

干杯, /r3副本

最佳答案

您需要在您的 xml 中设置默认命名空间,如下所示:

<?xml version="1.0" encoding="utf-8"  ?>
    <Assets xmlns="http://tempuri.org/data.xsd">
        <Asset>
            <FileName>Boomerang - Error codes.xlsx</FileName>
            <DisplayName>Boomerang - Error codes</DisplayName>
            <Description>This is the Boomerang error codes file</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Boomerang</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
        <Asset>
            <FileName>Issue Tracker v5.xlsx</FileName>
            <Description>This is the issue tracker for Skipstone</Description>
            <Tags>
                <Tag>Excel</Tag>
                <Tag>Skipstone</Tag>
            </Tags>
            <Categories>
                <Category>1</Category>
                <Category>4</Category>
            </Categories>
        </Asset>
    </Assets>

此外,还有一些其他问题:

路径属性未在架构中定义,“Assetd”元素未定义。 maxOccurs="unbounded"需要在 xs:element name="Asset"的模式中设置

如果您无法修改 xml,则需要从 xsd 中删除目标架构:

<xs:schema id="data"
    xmlns:mstns="http://tempuri.org/data.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>

然后像这样注册模式:

settings.Schemas.Add(null, "data.xsd");

关于C# XML Schema 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15741469/

相关文章:

c# - 正则表达式:匹配除 . 之外的任何标点符号。和 _

C# for(;;) 循环和数组

java - Spring 环境中的多个@ConfigurationProperties validator bean

用于验证用户在 App Engine 应用程序中输入的邮政地址的 Java 库?

javascript - 是否可以使用前端 JavaScript 编写 JSON 文件?

c# - ResolveAll 不工作

xml - XML 解析器是否区分 xsi :nil ="true" and omitted elements?

c# - 仅针对一个 namespace 解析 XML

c# - 在 C# 中解析 XML

java - 在 JSR-303 Bean 验证中获取 HttpServletRequest