使用 XPathNavigator 获取 ValueType

Get ValueType with XPathNavigator

我尝试使用 XPathNavigator 获取 XElement 的值类型,但 ValueType 总是 returns 作为类型 String。 我期望导航器 returns 是 XSD 文档中指定的 ValueType。

正在加载 xml 和 xsd:

XmlReaderSettings aSettings = new XmlReaderSettings();
aSettings.Schemas.Add(myXmlSchemaSet);
aSettings.ValidationType = ValidationType.Schema;

XmlReader aReader = XmlReader.Create(PathToXml, aSettings);
XDocument aDocument = XDocument.Load(aReader);
aDocument.Validate(aSchemaSet, null, true);

获取值类型:

XPathNavigator aNavigator = aDocument.CreateNavigator();
Type aElementType = aNavigator.SelectSingleNode(myXPath).ValueType;

XML to LINQ 不支持此功能,因为 XNodeNavigator 未实现 IXmlSchemaInfo 且未覆盖 XPathNavigator.SchemaInfo.

您可以使用支持该功能的 System.Xml。