时间:2019-03-17 标签:c#XMLSchemavalidation

2023-11-23

我有一个很好的 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 文件无效,因为第一个元素是 Assetd 而不是 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");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

时间:2019-03-17 标签:c#XMLSchemavalidation 的相关文章

随机推荐

  • 更改通用 Windows 平台中的按钮样式

    我尝试制作一个简单的 C UWP 应用程序 但我不知道当鼠标悬停在按钮上时如何删除灰色背景 我怎么做到这一点 请记住 它是适用于 Windows 10 平台的 UWP 而不是 Windows Phone 8 1 或 WPF 按着这些次序 在
  • Qt 检测计算机何时进入睡眠状态?

    如何检测用户计算机何时进入睡眠状态 笔记本电脑盖子关闭 由于不活动而进入睡眠模式等 我需要这样做来断开用户的 TCP 连接 基本上我们有一个简单的聊天应用程序 我们想让用户离线 Qt 无法检测计算机何时进入睡眠或休眠状态 但有一些依赖于平台
  • 对于数组,为什么会出现 a[5] == 5[a] 的情况?

    正如乔尔在书中指出的那样堆栈溢出播客 34 in C语言 又名 K R C 中提到了数组的这个属性 a 5 5 a Joel 说这是因为指针运算 但我还是不明白 为什么a 5 5 a C 标准定义了 运算符如下 a b a b 所以a 5
  • Django Rest框架-过滤多对多字段

    假设我有一个这样的模型 class Car models Model images models ManyToManyField Image class Image models Model path models CharField ty
  • ZF2 中的会话

    您能告诉我如何在 ZF2 中正确使用会话吗 到目前为止我有这个代码 session gt remember me seconds gt 2419200 use cookies gt true cookie httponly gt true
  • 在列表中创建重复的项目

    我有以下代码来复制列表的成员X times 虽然它有效 但感觉不是特别干净 实时代码示例 http rextester com UIVZVX7918 public static List
  • 如何通过方法参数加锁?

    string Get string key lock sync DoSomething 如果 DoSomething 仅依赖于密钥 我想要密钥依赖锁 我认为它可能是带有同步对象的字典 有没有完整的解决方案 就像真实的例子一样在 ASP NE
  • Instagram API 匹配代码未找到或已被使用

    我在使用 Instagram API 的实时服务器上看到此错误 Error true message Matching code was not found or was already used 我在这里阅读了一些清除缓存的建议 但这并不
  • Laravel:POST 方法返回 MethodNotAllowedHttpException

    我有一个POST路线在我的api php文件 它是这样的 Route group namespace gt api function Route post parent signup ParentController signUp 我正在尝
  • ejb 3.1 中的计时器服务 - 调度调用超时问题

    我使用 Singleton Schedule 和 Timeout 注释创建了简单的示例 以尝试它们是否能解决我的问题 场景是这样的 EJB 每 5 秒调用一次 检查 函数 如果满足某些条件 它将创建单个操作计时器 该计时器将以异步方式调用一
  • JavaFX:将控制台输出重定向到在 SceneBuilder 中创建的 TextArea

    EDIT 4 我创建了一个简单的示例 应该可以让您了解现在发生的情况 现在发生的情况是 每当我单击按钮将 HELLO WORLD 打印到 TextArea 时 程序就会挂起并使用 100 的 CPU Eclipse 控制台面板中也没有输出
  • 检查 COM 接口是否还存在?

    在 COM 中 如何验证指向 COM 对象的指针在另一端仍然具有有效的对象 我遇到一个问题 以下代码尝试检查是否m pServer指针仍然存在 但是当暴露该接口的应用程序被终止时 这段代码会使应用程序崩溃 有人可以建议如何在使用前检查指针吗
  • 在 Activity 的 onDestroy 方法中保存数据

    我正在编写一个任务列表并拥有 Project 对象 其中包含所有任务 和元数据 我使用操作日志 因此当任务发生变化时 我不会立即将其保存到数据库中 而是将其保留在内存中 以便在活动完成时转储到数据库中 Activity 的 onDestro
  • Django 异步处理

    我有一堆 Django 请求 它们执行一些数学计算 用 C 编写并通过 Cython 模块执行 这可能需要不确定的时间 大约 1 秒 来执行 此外 请求不需要访问数据库 并且彼此独立且独立于 Django 现在一切都是同步的 使用 Guni
  • 如何拖动 NSStatusItems

    大家都知道 Mac OS X 中的菜单栏 或者更好的说法是 NSStatusBar 有些物品我可以移动 有些则不能 我希望能够拖动我的应用程序的 NSStatusItem 知道如何实现吗 尽管 NSStatusItems 出现在 Apple
  • 软堆:什么是损坏以及它为什么有用?

    我最近读了 Bernard Chazelle 的论文 The Soft Heap An Approximate Priority Queue with Optimal Error Rate by Bernard Chazelle http
  • 如何对部分路径使用 .htaccess 重定向?

    我必须调整网站上的一些路径 并且需要使用 htaccess 在用户访问旧网址时重定向项目 例如我的旧网址 相对 可能是 old path page1 php old path page2 php old path page3 php etc
  • 如何快速设置栏按钮的图像?

    我正在尝试为栏按钮项目设置一个图像 因为我有一个像这样的图像 分辨率为 30 30 但当我将此图像分配给 栏 按钮时 它看起来像 我已经这样分配图像 如果我尝试这种方式 例如为按钮制作 IBOutlet 并以编程方式设置图像this问题和代
  • 在新的材料设计中,是否有一个官方 API 用于在工具栏上居中标题,就像流行的 Android 应用程序一样?

    背景 过去 Google 总是显示工具栏以使标题左对齐 https material io develop android components app bar layout 然而 最近 似乎在它的一些应用程序上 标题居中 即使它的左右没有
  • 时间:2019-03-17 标签:c#XMLSchemavalidation

    我有一个很好的 XML 文件 如下所示