Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带值和模式检查的XML元素 可能不需要但为了完整性而包含的背景信息_Xml_Xml Parsing - Fatal编程技术网

不带值和模式检查的XML元素 可能不需要但为了完整性而包含的背景信息

不带值和模式检查的XML元素 可能不需要但为了完整性而包含的背景信息,xml,xml-parsing,Xml,Xml Parsing,我正在开发一个系统,其中所有用户输入都将存储在一个xml文件中。目前,用户输入由aspx.net web表单处理。在C#code behind中,我将这些值存储在一个对象中,然后序列化回XML,这些值也将从XML序列化回一个对象,然后显示给用户进行编辑 问题 数据使用以下模式以XML表示(我对.Net特定前缀表示赞同) 使用对XML文件运行xmllint并使用XSD,我没有收到错误。嗯。。。真奇怪。我已经添加了验证方法的描述(还有代码块)。我怀疑我调用的是.net验证代码,所以我想知道我是否做得

我正在开发一个系统,其中所有用户输入都将存储在一个xml文件中。目前,用户输入由aspx.net web表单处理。在C#code behind中,我将这些值存储在一个对象中,然后序列化回XML,这些值也将从XML序列化回一个对象,然后显示给用户进行编辑

问题 数据使用以下模式以XML表示(我对.Net特定前缀表示赞同)


使用
对XML文件运行
xmllint
并使用XSD,我没有收到错误。嗯。。。真奇怪。我已经添加了验证方法的描述(还有代码块)。我怀疑我调用的是.net验证代码,所以我想知道我是否做得都不对,因为我从未使用过.net,所以我对.net代码帮助不大。我仔细检查了
xmllint
是否有bug。您的文档也在那里验证。可能您没有使用您认为正在使用的模式,或者没有使用您认为正在使用的输入。(在测试过程中,我确实偶尔会遇到一些问题。为了临时检查而更改了一些内容,忘记撤销它,等等。)@Louis我很感谢你的评论,但我已经剥离了可能的其他XML模式,可以确认我只使用了需要的模式,并且我也在根据正确的输入文件进行验证)。
<?xml version="1.0" encoding="utf-8"?>
  <xsd:schema id="UserInput-Schema"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="urn:UserInput-Schema"
    elementFormDefault="qualified">

     <xsd:element name="userInputData">
       <xsd:complexType>
        <xsd:sequence>

          <xsd:element name="someTextData" type="xsd:string"
            minOccurs="1" maxOccurs="1" />

          <xsd:element name="someOtherTextData" type="xsd:string"
            minOccurs="1" maxOccurs="1" />

          <xsd:element name="yetMoreTextData" type="xsd:string"
            minOccurs="1" maxOccurs="1" />


        </xsd:sequence>
       </xsd:complexType>
     </xsd:element>

  </xsd:schema>
<?xml version="1.0" encoding="utf-8"?>
  <userInputData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

         <someTextData>I love XML!</someTextData>
         <someOtherTextData/>
         <yetMoreTextData>More text</yetMoreTextData>

  </userInputData>
<?xml version="1.0" encoding="utf-8"?>
  <userInputData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">

         <someTextData>I love XML!</someTextData>
         <someOtherTextData>Input not supplied<someOtherTextData>
         <yetMoreTextData>More text</yetMoreTextData>

  </userInputData>
<someTextValue />
private static void ValidateAgainstSchema(Stream schemaToUse, XmlReader documentToValidate)
{
  XmlSchema schema;
  // Ensure that the stream to the schema is read from the very
  // begining of the file.
  schemaToUse.Position = 0;

  // Read the schema from the stream into the XmlSchema object
  using (XmlReader schemaReader = XmlReader.Create(schemaToUse))
  {
    schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
  }

  // Add the schema to a set of schemas
  XmlSchemaSet schemas = new XmlSchemaSet();
  schemas.Add(schema);

  // Set up the schema reader settings
  XmlReaderSettings settings = new XmlReaderSettings();
  settings.ValidationType = ValidationType.Schema;
  settings.Schemas = schemas;
  settings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.ReportValidationWarnings;
  settings.ValidationEventHandler += ValidationEventHandler;

  // Run through the xml file to be checked, before passing control back
  // to the calling method
  using (var validationReader = XmlReader.Create(documentToValidate, settings))
  {
    while (validationReader.Read())
    {
      // If we fail to read the xml file (badly formed, doesn't match
      // the schema, etc.) then we'll fall through to the event handler
      // (readerSettings_ValidationEventHandler) automatically.
    }
  }
}

private static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
  if (e.Severity == XmlSeverityType.Warning)
  {
    /* warning while parsing XML file */
    // warnings can be ignored for now. These are not critical
  }
  else if (e.Severity == XmlSeverityType.Error)
  {
    /* Error while parsing XML file */
    // Errors are problems with the xml file (when checked against
    // the schema). These are critical issues with the conents of
    // the XML file.
    // Possibly throw an exception?

    throw new Exception(e.Message);
  }
}