我在 Visual Studio 中使用 XSD,但结果与预期不符。
我对一个元素有一个独特的约束。当我故意通过引入重复来破坏 Xml 时,intellisense 会突出显示错误,但是当它让我编译和运行代码时,没有进一步的事情发生,并且错误被允许级联。
是否有可能我错过了任何事情或做错了什么,或者这是就 XSD 中的错误验证而言?
public static T CreateClassFromXml<T>(string path, string root, XmlReaderSettings settings)
where T : class
{
path.ThrowNullOrEmpty("path");
settings.ThrowNull("settings");
File.Exists(path).ThrowFalse(string.Format("'{0}' could not be found", path));
var serializer = new XmlSerializer(typeof(T), new XmlRootAttribute() { ElementName = root });
using (var reader = XmlReader.Create(path, settings))
{
return (T)serializer.Deserialize(reader);
}
}
调用代码:
var xsd = context.Server.MapPath(Paths.Xml + "Wikis.xsd");
File.Exists(xsd).ThrowFalse(string.Format("'{0}' could not be found", xsd));
var settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsd);
settings.ValidationType = ValidationType.Schema;
var xml = context.Server.MapPath(Paths.Xml + "Wikis.xml");
var metaList = XmlHelper.CreateClassFromXml<List<WikiMeta>>(xml, "AllMeta", settings);