Can i specify a schema to build an XDocument while loading a xml file? <p>I am a xml newbie trying to create a XDocument type from a xml file.</p> <p>I can validate the xml against a schema.</p> <pre><code>public class XmlHandler { public XDocument Read(string filename, string schemaname) { var schemas = this.GetSchemas(schemaname); var doc = XDocument.Load(filename); var invalid = false; doc.Validate(schemas, (o, args) =&gt; { this.OnValidationErrors(o, args); invalid = true; }); return invalid ? new XDocument() : doc; } public XmlSchemaSet GetSchemas(string schemaname) { var schemas = new XmlSchemaSet(); schemas.Add(null, schemaname); return schemas; } private void OnValidationErrors(object sender, ValidationEventArgs e) { Debug.Print("Errors: ", e); } } </code></pre> <p>But the structure of the the XDocument seems to be wrong.</p> <p>When running this code</p> <pre><code> [Fact] public void Read_get_elements() { var sut = new XmlHandler(); var result = sut.Read(this.TestFile, this.TestFileSchema); var root = result.Root; var elements = result.Elements(); var nodes = result.Nodes(); var descendants = result.Descendants(); Assert.NotEmpty(elements); } </code></pre> <p>the root variable contains the complete xml string and the the other IEnumerable variables stay empty. What am i missing?</p> <p>EDIT: This is part of the xml and the xsd</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html" xmlns="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html" elementFormDefault="qualified"&gt; &lt;xs:include schemaLocation="eurex_reports_common_structs.xsd"/&gt; &lt;xs:complexType name="cb020Type"&gt; &lt;xs:annotation&gt; &lt;xs:documentation&gt;CB020 Position Summary&lt;/xs:documentation&gt; &lt;/xs:annotation&gt; &lt;xs:sequence&gt; &lt;xs:element name="rptHdr" type="rptHdrType" /&gt; &lt;xs:element name="cb020Grp" type="cb020GrpType" minOccurs="0" maxOccurs="unbounded" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;xs:element name="cb020" type="cb020Type"/&gt; &lt;xs:complexType name="cb020GrpType"&gt; &lt;xs:sequence&gt; &lt;xs:element name="cb020KeyGrp" type="cb020KeyGrpType" /&gt; &lt;xs:element name="cb020Grp1" type="cb020Grp1Type" minOccurs="1" maxOccurs="unbounded" /&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>And this is part of the xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;cb020 xmlns="https://www.eurexchange.com/members/releases/eurex14/manuals_technical_en.html"&gt; &lt;rptHdr&gt; &lt;exchNam&gt;EUREX&lt;/exchNam&gt; &lt;envText&gt;P&lt;/envText&gt; &lt;rptCod&gt;CB020&lt;/rptCod&gt; &lt;rptNam&gt;Position Summary&lt;/rptNam&gt; &lt;membLglNam&gt;Cyberdyne Systems&lt;/membLglNam&gt; &lt;rptPrntEffDat&gt;2011-12-05&lt;/rptPrntEffDat&gt; &lt;rptPrntRunDat&gt;2011-12-05&lt;/rptPrntRunDat&gt; &lt;/rptHdr&gt; &lt;cb020Grp&gt; &lt;cb020KeyGrp&gt; ... &lt;/cb020KeyGrp&gt; &lt;cb020Grp1&gt; ... &lt;/cb020Grp1&gt; &lt;/cb020Grp&gt; &lt;/cb020&gt; </code></pre>