【发布时间】:2014-01-14 13:32:12
【问题描述】:
我正在尝试验证 XML 文件并向用户显示任何错误。
简化示例:
XML:
<MyApps>
<MyApp>
<Header>
<Name>Name 1</Name>
<Rate>4</Rate>
</Header>
<Players>
<Player>
<Name>Bert</Name>
<Age>22</Age>
</Player>
<Player>
<Name>Harry</Name>
<Age>12</Age>
</Player>
<Player>
<Name>George</Name>
<Age>16</Age>
</Player>
</Players>
</MyApp>
<MyApp>
<Header>
<Name>Name 2</Name>
<Rate>3</Rate>
</Header>
<Players>
<Player>
<Name>Fred</Name>
<Age>29</Age>
</Player>
<Player>
<Name>Bill</Name>
<Age>19</Age>
</Player>
<Player>
<Name>Garry</Name>
<Age>20</Age>
</Player>
</Players>
</MyApp>
</MyApps>
验证器:
public class XMLValidationMessage
{
public int LineNumber;
public int LinePosition;
public string Message;
public XmlSeverityType Severity;
}
public class XMLValidationResponse
{
/// <summary>
/// Result of the XML Validation.
/// </summary>
[Description("Result of the XML Validation")]
public bool IsXMLValid { get; set; }
/// <summary>
/// XML Validation Errors.
/// </summary>
[Description("XML Validation Errors")]
public List<XMLValidationMessage> ValidationErrors { get; set; }
public XMLValidationResponse()
{
ValidationErrors = new List<XMLValidationMessage>();
}
}
private static XMLValidationResponse response = new XMLValidationResponse();
public static XMLValidationResponse ValidateXML(string XMLFile, string XSDFile)
{
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null, XSDFile);
response.IsXMLValid = true;
Validate(XMLFile, schemaSet);
return response;
}
private static void Validate(String filename, XmlSchemaSet schemaSet)
{
XmlSchema compiledSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
compiledSchema = schema;
}
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(compiledSchema);
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings.ValidationType = ValidationType.Schema;
//Create the schema validating reader.
XmlReader vreader = XmlReader.Create(filename, settings);
while (vreader.Read()) { }
//Close the reader.
vreader.Close();
}
private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
response.IsXMLValid = false;
if (args.Severity == XmlSeverityType.Warning)
{
XMLValidationMessage xmlm = new XMLValidationMessage();
xmlm.Severity = XmlSeverityType.Warning;
xmlm.LineNumber = 0;
xmlm.LinePosition = 0;
xmlm.Message = "Matching schema not found. No validation occurred.";
response.ValidationErrors.Add(xmlm);
}
else
{
XMLValidationMessage xmlm = new XMLValidationMessage();
xmlm.Severity = XmlSeverityType.Error;
xmlm.LineNumber = args.Exception.LineNumber;
xmlm.LinePosition = args.Exception.LinePosition;
xmlm.Message = args.Message;
response.ValidationErrors.Add(xmlm);
}
}
如果 XML 验证失败,我会得到 XMLValidationMessage 形式的错误列表。这给了我一个行号、行位置和消息。但是,似乎没有办法将错误链接到导致错误的 XML 元素。验证后,我将 XML 反序列化为数据类对象,然后使用它来填充树视图。最终我想要做的是突出显示未通过验证的树视图的分支。我在想,也许我正在以错误的方式解决这个问题?
更新 - 错误示例
ValidationErrors Count = 14 System.Collections.Generic.List<MyApp.Xml.XMLValidationMessage>
- [0] {MyApp.Xml.XMLValidationMessage} MyApp.Xml.XMLValidationMessage
LineNumber 89 int
LinePosition 127 int
Message "The 'http://www.myweb.co.uk/srm/mscc:Remarks' element is invalid - The value 'Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...' is invalid according to its datatype 'http://www.MyWeb.co.uk/srm/mscc:String50Type' - The actual length is greater than the MaxLength value." string
Severity Error System.Xml.Schema.XmlSeverityType
我得到了一个错误集合,这些错误都是有效的错误。不确定模式是否有助于该部分正常工作。我遇到的麻烦是将错误链接到导致错误的 XML 元素。
【问题讨论】:
-
能否请您发布一个您收到的错误示例?架构也很有用。
标签: c# xml validation xml-serialization xml-validation