XElement.GetSchemaInfo() is returning null, Why?

2019-07-13 08:08发布

问题:

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.