【问题标题】:Validating XML string using inline XML schema with XmlReader使用带有 XmlReader 的内联 XML 模式验证 XML 字符串
【发布时间】:2013-08-03 11:53:23
【问题描述】:

我想使用内联 XML 模式验证字符串,并想处理 XMLSchemaException。我尝试了以下代码:

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);
        }

我在传递无效参数while (xmlrdr.Read()); 时收到错误“XMLException was unhandled”。但我只想处理XMLSchemaValidationException。请告诉我如何实现这一目标。

【问题讨论】:

  • 什么是ab?你说的是参数吗? XmlException 说明了什么问题?
  • 谢谢我把ab改成参数了,XMLEXception没有在while (xmlrdr.Read());

标签: c# xmlreader


【解决方案1】:

你必须添加一个 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);
 }

【讨论】:

  • Th@rene :anks 但是 element name=""bla" 我们需要传递我们拥有的所有元素还是只传递根元素。
  • 我添加了与给定 XML 匹配的完整架构
  • :谢谢,如果我想在我的字符串中添加更多元素,并在这种情况下检查如何添加 tose 元素,因为我的字符串可能包含更多元素,用户可以提供更多元素。我可以创建通过读取我的字符串自动架构。
猜你喜欢
  • 1970-01-01
  • 2011-07-23
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2014-03-16
  • 2013-05-22
相关资源
最近更新 更多