XElement.GetSchemaInfo() is returning null, Why?

爱⌒轻易说出口 提交于 2019-12-11 03:36:05

问题


I'm trying to validate an XDocument with a compiled schema that I have (which works) but when I try to access PSVI for the root XElement it returns null. I need this so I can validate child XElements.

Here is a sample code of what I'm trying to do:


XDocument xmlDoc = XDocument.Load(xmlFilePath);
XElement root = _xmlDoc.Elements().Single();
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(schema);
xmlDoc.Validate(schemas, ValidationEventHandler);
XmlSchemaElement se = xmlDoc.Elements().Single().GetSchemaInfo();

I can see that validation for XDocument works, I catch ValidationEvents and all.

All thoughts are appreciated. Thank you.


回答1:


There's another overload for Validate (http://msdn.microsoft.com/en-us/library/bb354954(v=VS.90).aspx) which takes a boolean parameter. If you set that boolean to true, the PSVI will be stored on the nodes and then GetSchemaInfo should work. Without the PSVI in the tree it can't work.



来源:https://stackoverflow.com/questions/3167070/xelement-getschemainfo-is-returning-null-why

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!