When reading XSD files, there are two things to consider:
- take the namespace (using XElement.GetDefaultNamespace)
- take the prefix (using XElement.GetNamespaceOfPrefix)
You need to find your way manually in the XSD file:
- find the complexType node
- find the sequence
- find the element
- and so on
It is not difficult, but tedious. If you will need this often, than you could implement the methods as extensions. The code that follows could be optimised regarding the error handling.
A possible solution using LINQ2XML can look like this:
try
{
var xsd = XDocument.Load("d:\\temp\\input.xsd");
var ns = xsd.Root.GetDefaultNamespace();
var prefix = xsd.Root.GetNamespaceOfPrefix("xs");
// get Vehicle
var vehicle = xsd.Root.Element(prefix + "element");
// get sequence for Ford
var sections = vehicle.Element(prefix + "complexType")
.Element(prefix + "sequence")
// the Ford element
.Element(prefix + "element")
.Element(prefix + "complexType")
.Element(prefix + "sequence")
// elements
.Elements(prefix + "element").ToList();
foreach (var section in sections)
{
Console.WriteLine(section.Attribute("name").Value);
// for each section element
var items = section.Element(prefix + "complexType")
.Element(prefix + "sequence")
// take the test elements
.Elements(prefix + "element");
foreach (var item in items)
{
Console.WriteLine(item.Attribute("name").Value);
// access another attributes or values here
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
The output is:
SectionA
AX010_1
AX040_1
AX050_1
AX190_1
A080_1
AX230F_1
AX230G_1
AX230GNOTE_1
AX230H_1
AX230HNOTE_1
AX230J_1
SectionB
BX010_1
BX070_1
...