我想验证使用内联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
。请让我知道我该如何实现这一点。
什么是AB?你的意思是参数吗? XmlException说什么是什么问题? – rene
谢谢我已将ab更改为参数,XMLEXception未在while(xmlrdr.Read())处理; – channa