2011-11-13 50 views
2

我的目標: 我想搜索大型XML Schema規範中的元素。來自多個XSD文件的複合(使用導入)。LINQ - 到XML架構 - 導入多個XSD文件(.NET)

可以使用LINQ嗎?如何將整個SOM(模式對象模型)存儲到內存中然後詢問?

我想:

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

但我不知道如何在這裏使用LINQ。

回答

1

管理多個模式的最好方法是使用XmlSchemaSet;將你的模式添加到一個XmlSchemaSet然後編譯它。這應該回答你的「SOM進入記憶」。

對於如何使用LINQ來編譯XmlSchemaSet,它非常依賴於您嘗試解決的問題類型。例如,假設您試圖獲取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(); 

我希望這給你一個想法...