2
A
回答
2
您可以使用XmlSchemaCollection.Add(string, XmlReader)
:
string file = "Assembly.Namespace.FileName.ext";
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add(null, new XmlTextReader(
this.GetType().Assembly.GetManifestResourceStream(file)));
2
這裏是我的:
public static bool IsValid(XElement element, params string[] schemas)
{
XmlSchemaSet xsd = new XmlSchemaSet();
XmlReader xr = null;
foreach (string s in schemas)
{
xr = XmlReader.Create(new MemoryStream(Encoding.Default.GetBytes(s)));
xsd.Add(null, xr);
}
XDocument doc = new XDocument(element);
var errored = false;
doc.Validate(xsd, (o, e) => errored = true);
return !errored;
}
,您可以通過使用它:
var xe = XElement.Parse(myXmlString); //by memory; may be wrong
var result = IsValid(xe, MyApp.Properties.Resources.MyEmbeddedXSD);
這ISN」保證即,這是100%;它只是一個很好的起點。 XSD驗證是不是我完全在...
1
看看它是如何完成在Winter4NET。完整的源代碼is here。基本代碼摘錄:
Stream GetXsdStream() {
string name = this.GetType().Namespace + ".ComponentsConfigSchema.xsd";
return Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
}
...
XmlSchema schema = XmlSchema.Read(GetXsdStream(), null);
xmlDoc.Schemas.Add(schema);
xmlDoc.Validate(new ValidationEventHandler(ValidationCallBack));
謝謝你,我想出如何落實「這裏...您的資源流......」而現在它的工作原理 – Drake 2009-10-20 14:05:36
我怎麼能改善這個答案,馬可? – 2009-10-20 14:07:31
我認爲是好的,但可能你可以描述或添加一個鏈接如何從程序集 – Drake 2009-10-20 14:45:16