2013-08-03 58 views
1

我想验证使用内联XML架构的字符串并希望处理XMLSchemaException。我想下面的代码:使用内联XML架构和XmlReader验证XML字符串

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是未处理”。但我只想处理XMLSchemaValidationException。请让我知道我该如何实现这一点。

+0

什么是AB?你的意思是参数吗? XmlException说什么是什么问题? – rene

+0

谢谢我已将ab更改为参数,XMLEXception未在while(xmlrdr.Read())处理; – channa

回答

0

你要添加的ValidationEventHandler

String  [email protected]"<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); 
} 
+0

Th @ rene:anks但是元素名称=“”bla“是否需要传递我们拥有的所有元素或只有根元素 – channa

+0

我添加了与给定的XML匹配的完整模式 – rene

+0

:谢谢,如果我想添加更多元素添加到我的字符串,并检查该情况下如何添加tose元素,因为我的字符串可能包含更多元素用户可以给一些elemets.can我通过阅读我的字符串自动创建模式。 – channa