在.NET框架具有schema object model (SOM)和有钩访问模式类型,同时用一个XmlReader或验证System.Xml.XmlDocument或System.Xml.Linq.XDocument后验证。下面是展示如何验证System.Xml.Linq.XDocument以及如何然后访问架构信息的样本:
Dim doc As XDocument = XDocument.Load("..\..\XMLFile1.xml")
Dim schemaSet As New XmlSchemaSet()
schemaSet.Add(Nothing, "..\..\XMLFile1.xsd")
doc.Validate(schemaSet, Nothing, True)
For Each leafElement As XElement In doc.Descendants().Where(Function(d) Not (d.Elements().Any()))
Console.WriteLine("Element named {0} has type {1}", leafElement.Name, DirectCast(leafElement.GetSchemaInfo().SchemaType, XmlSchemaSimpleType).Datatype.TypeCode)
Next
随着XML文件是
<?xml version="1.0" encoding="utf-8" ?>
<persons>
<person>
<last-name> Watson </last-name>
<foo>false</foo>
</person>
</persons>
和架构是
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="persons">
<xs:complexType>
<xs:sequence>
<xs:element name="person" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="last-name" type="xs:normalizedString" />
<xs:element name="foo" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
该样本输出
Element named last-name has type NormalizedString
Element named foo has type Boolean
因此,请浏览MSDN或本地VS文档中的SOM文档,您应该能够以这种方式查找信息。
谢谢。我接受了另一个答案,因为1.这使我走上了正确的道路。在我想清楚之前,我没有看到你的答案。虽然我认为你的回答在我的具体问题上更加完整。 – ElGringoGrande 2011-02-17 18:43:40