我的函数在XDocument构造函数中爆炸。说不允许使用非空白字符。
public static bool ValidateXML(string xml, string xsd)
{
StringBuilder sb = new StringBuilder();
bool blnReturn = true;
bool errors = false;
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new StringReader(xsd)));
XDocument xmlDoc = new XDocument(XmlReader.Create(new StringReader(xml)));
//validate the XML text against the XSD
xmlDoc.Validate(schemas, (o, e) =>
{
sb.AppendLine(e.Message);
errors = true;
});
//if there are errors, display them and return false
if (errors)
{
MessageBox.Show("XML did not parse cleanly. Please fix the following errors.\n\n" + sb.ToString(), "XML Parsing Results");
blnReturn = false;
}
else
{
MessageBox.Show("XML Parsed cleanly. Saving to file.", "XML Parsing Results");
}
return blnReturn;
}
答案 0 :(得分:0)
我没有看到代码有任何问题。您的xml字符串可能无效。非空白字符错误表示您的xml元素之间有文本字符。例如:
<blah>This text is okay</blah>This text isn't
<blah>More text</blah>