我的应用程序中有以下XML解析代码:
public static XElement Parse(string xml, string xsdFilename)
{
var readerSettings = new XmlReaderSettings
{
ValidationType = ValidationType.Schema,
Schemas = new XmlSchemaSet()
};
readerSettings.Schemas.Add(null, xsdFilename);
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
readerSettings.ValidationEventHandler +=
(o, e) => { throw new Exception("The provided XML does not validate against the request's schema."); };
var readerContext = new XmlParserContext(null, null, null, XmlSpace.Default, Encoding.UTF8);
return XElement.Load(XmlReader.Create(new StringReader(xml), readerSettings, readerContext));
}
Run Code Online (Sandbox Code Playgroud)
我用它来解析发送到我的WCF服务的字符串到XML文档,用于自定义反序列化.
当我读入文件并通过网络发送它们时(请求),它工作正常; 我已经确认没有发送BOM.在我的请求处理程序中,我正在序列化响应对象并将其作为字符串发送回来.序列化过程将UTF-8 BOM添加到字符串的前面,这会导致在解析响应时中断相同的代码.
System.Xml.XmlException : Data at the root level is …Run Code Online (Sandbox Code Playgroud)