简体   繁体   English

使用内联XML模式和XmlReader验证XML字符串

[英]Validating XML string using inline XML schema with XmlReader

I want to validate a string using an inline XML schema and want to handle XMLSchemaException. 我想使用内联XML模式验证字符串,并想处理XMLSchemaException。 I tried the code below: 我尝试了下面的代码:

String parameter="<HostName>Arasanalu</HostName><AdminUserName>Administrator</AdminUserName>
    <AdminPassword>A1234</AdminPassword><PlaceNumber>38</PlaceNumber>"

    // Set the validation settings.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.ValidationType = ValidationType.Schema;
    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
    settings.ValidationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes;

    //settings.Schemas.Add(null,"http://www.w3.org/2001/XMLSchema");
    XmlSchemaSet sch = new XmlSchemaSet();
    sch.Add(null,"http://www.w3.org/2001/XMLSchema");
    try
    {
        // Create the XmlReader object.
        XmlReader xmlrdr = XmlReader.Create(new StringReader("<root>" + parameter + "</root>"),
            settings);
            // Parse the file. 
            while (xmlrdr.Read());
        }
        catch (XmlSchemaValidationException ex)
        {
            Console.WriteLine("The file could not read the value at XML  format is not correct due to" + ex);
        }

I was getting the error "XMLException was unhandled" when passing an invalid parameter while (xmlrdr.Read()); while (xmlrdr.Read());传递无效参数时,出现错误“未处理XMLException” while (xmlrdr.Read()); . But I only want to handle XMLSchemaValidationException . 但是我只想处理XMLSchemaValidationException Please let me know how I can achive this. 请让我知道如何实现此目标。

You have to add an ValidationEventHandler 您必须添加ValidationEventHandler

String     parameter=@"<HostName>Arasanalu</HostName>
                             <AdminUserName>Administrator</AdminUserName>
                             <AdminPassword>A1234</AdminPassword>
                             <PlaceNumber>38</PlaceNumber>";

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing= DtdProcessing.Parse;
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.AllowXmlAttributes |
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessSchemaLocation;

settings.Schemas.Add(null,XmlReader.Create(new StringReader(
@"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
<xs:element name=""root"">
    <xs:complexType>
      <xs:sequence>
        <xs:element name=""HostName"" type=""xs:string"" />
        <xs:element name=""AdminUserName"" type=""xs:string"" />
        <xs:element name=""AdminPassword"" type=""xs:string"" />
        <xs:element name=""PlaceNumber"" type=""xs:positiveInteger"" />
      </xs:sequence>
    </xs:complexType>
</xs:element></xs:schema>"), settings));

settings.ValidationEventHandler += (s,e) => 
      { 
        throw new XmlSchemaValidationException(e.Message); 
      };
try
{
  // Create the XmlReader object.
  XmlReader xmlrdr = XmlReader.Create(
                  new StringReader("<root>" + parameter + "</root>"), settings);
  // Parse the file. 
  while (xmlrdr.Read());
}
catch (XmlSchemaValidationException ex)
{
  Console.WriteLine(
          "The file could not read the value at XML  format is not correct due to" + ex);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM