I am having a heck of a time trying to parse a schema file… was hoping someone could help.
Here is what the xsd looks like
<xs:element name="E1">
<xs:complexType>
<xs:sequence>
<xs:element name="E2" nillable="true" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="E3" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="E4" nillable="true" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="E5" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="A1" type="xs:integer" use="optional" />
<xs:attribute name="A2" type="xs:string" use="optional" />
<xs:attribute name="A3" type="xs:string" use="optional" />\
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
</xs:complexType>
</xs:element>
This isn’t complete, but enough to get the idea. Here is the code I was trying to get the element name, followed by any attributes if they existed, but this is only returning the element names.
var xs = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var doc = XDocument.Load(sourceName + sourceApi + "Input.txt");
foreach (var el in doc.Descendants(xs + "element"))
{
Trace.WriteLine("ANDY ------ " + el.Attribute("name").Value);
foreach (var attr in el.Elements(xs + "attribute"))
{
Trace.WriteLine(attr.Attribute("name").Value);
}
}
This is outputing
Andy —– E1
Andy —– E2
Andy —– E3
Andy —– E4
Andy —– E5
Where I want it to output
Andy —– E1
Andy —– E2
Andy —– E3
Andy —– E4
Andy —– E5
A1
A2
A3
Thanks in advance.
el.Elementschecks only the child elements, so there it return nothing. You should useDescendantsor specify full path byel.Elements(xs + "complexType").Elements(xs + "attribute"):