LINQ - 到XML Schema - 导入多个XSD文件(.NET)

时间:2011-11-13 14:14:10

标签: linq import xsd schema

我的目标: 我想在大型XML Schema规范中搜索元素。来自多个XSD文件的复合(使用导入)。

是否可以使用LINQ?如何将整个SOM(模式对象模型)存储到内存中,然后询问?

我试过了:

Dim reader As XmlTextReader = New XmlTextReader(path)
Dim myschema As XmlSchema = XmlSchema.Read(reader, AddressOf ValidationCallback)

但我不知道如何在这里使用LINQ。

1 个答案:

答案 0 :(得分:1)

管理多个模式的最佳方法是使用XmlSchemaSet;将您的架构添加到XmlSchemaSet然后编译它。这应该回答你的“SOM进入记忆”。

关于如何对已编译的XmlSchemaSet使用LINQ,它在很大程度上取决于您尝试解决的问题类型。例如,假设您正在尝试获取XML命名空间中的所有元素。你可能会写这样的东西(我意识到我已经用C#表达了它,我希望你能用它)。

XmlSchemaSet xset = new XmlSchemaSet();
xset.Add(XmlSchema.Read(...);
xset.Compile();
var query = from XmlSchemaElement element in xset.GlobalElements.Values where element.QualifiedName.Namespace == "urn:tempuri-org:mine" select element;            
foreach(XmlSchemaElement element in query) DoSomething();

另一个例子可能是使用Distinct子句来收集构成你的集合的XML命名空间集。

List<string> query1 = (from XmlSchema schema in xset.Schemas() select schema.TargetNamespace).ToList();
IEnumerable<string> xmlns = query1.Distinct();

我希望这些能给你一个想法......