2

My Goal: I would like to search for elements in large XML Schema spec. compound from multiple XSD files (with use of import).

Is is possible to use the LINQ? How can I store whole SOM (schema object model) into memory and then ask?

I tried:

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

But I have no idea how to use the LINQ here.

Daniel
  • 113
  • 2
  • 11

1 Answers1

1

The best way to manage multiple schemas is to use an XmlSchemaSet; add your schema(s) to an XmlSchemaSet and then compile it. This should answer your "SOM into memory".

For how to use LINQ against a compiled XmlSchemaSet, it very much depends on the type of problem you're trying to solve. For example, let's say you're trying to get all the elements in an XML namespace. You might write something like this (I realize I've phrased it in C#, I hope you're ok with it).

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();

Another example might be use the Distinct clause to collect the set of XML namespaces that make up your set.

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

I hope these give you an idea...

Petru Gardea
  • 21,373
  • 2
  • 50
  • 62