jjzjj

minOccurs ="0"元素上的 C# XSD 验证失败

coder 2024-07-02 原文

我正在针对此 XSD 验证一些 XML:

  <xs:element name="Composite">
    <xs:complexType>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:element name="HeldCurrency" type="mstns:HeldCurrencyType"  nillable="true"/>
        <xs:element name="Component" type="mstns:ComponentType" nillable="true" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute  name="Type" type="mstns:compositeType" use="required"/>
    </xs:complexType>x
  </xs:element>

  <xs:complexType name="HeldCurrencyType">
    <xs:attribute  name="CashPerCreationUnit" type="xs:double" />
    <xs:attribute  name="CashCurrency" type="xs:string" />
    <xs:attribute  name="ClosingSpotFx" type="xs:double" />
  </xs:complexType>

  <xs:complexType name="ComponentType">
    <xs:choice>
      <xs:element name="Weight" type="xs:double"/>
      <xs:element name="Units" type="xs:int"/>
    </xs:choice>
    <xs:attribute name="Symbol" type="xs:string" />
  </xs:complexType>

  <xs:simpleType name="compositeType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Index" />
      <xs:enumeration value="Etf" />
    </xs:restriction>
  </xs:simpleType>

当我针对它运行以下 XML 时:

<?xml version="1.0"?>
<Composite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Type="Etf" xmlns="urn:xxx-au:index-types">
  <HeldCurrency />
  <Component />
</Composite>

我使用以下代码在 C#/.Net4.0 中运行它:

using (FileStream fileStream = File.OpenRead("./Xml/Components.xsd"))
{
    using (var schemaReader = new XmlTextReader(fileStream))
    {
        settings.Schemas.Add(null, schemaReader);
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationEventHandler += OnValidationEventHandler;

        using (FileStream infile = File.OpenRead(filename))
        {
            using(var xmlReader = XmlReader.Create(infile,settings))
            {
                var serialiser = new XmlSerializer(typeof (Composite));
                _composite = (Composite) serialiser.Deserialize(xmlReader);
            }
        }
    }
}

我收到以下错误: 命名空间“urn:xxx-au:index-types”中的元素“Component”内容不完整。预期的可能元素列表: namespace 中的“重量,单位”。 但是,我有 minOccurs="0"那么为什么这会成为一个问题呢?毕竟序列不包含 ComponentType 元素是合法的,对吧?

提前致谢。

最佳答案

元素 Component 的 minOccurs 是 0 不是它的子元素..

所以试试这个:

  <xs:complexType name="ComponentType">
    <xs:choice>
      <xs:element name="Weight" type="xs:double" minOccurs="0"/>
      <xs:element name="Units" type="xs:int" minOccurs="0"/>
    </xs:choice>
    <xs:attribute name="Symbol" type="xs:string" use="optional"/>
  </xs:complexType>

我将 Weight 和 Units 的出现定义为“0”的地方也将属性设为可选!

关于minOccurs ="0"元素上的 C# XSD 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946592/

有关minOccurs ="0"元素上的 C# XSD 验证失败的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  4. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  5. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  6. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  7. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  8. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  9. ruby-on-rails - 如何将验证与模型分开 - 2

    我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:

  10. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

随机推荐