title: Linq to XML Namespace problems
body: <p>Greetings,</p>
  
  <p><strong>My goal:</strong> To validate an XML document then load the data into a custom object.  I'm using Linq to XML.</p>
  
  <p><strong>My situation:</strong> I'm struggling with the Namespace and/or the Linq syntax.  I thought everything was working.  The code reads the XML and loads the object, but realized the XDocument.Validate was passing everything through and not really validating.  I guess that's lax validation.  To get the XDocument.Validate() method to validate, I added a Namespace to the XML file.  The Validation works, but now the XElement created by my Linq Query returns null when trying to access .Element("Field").value.</p>
  
  <p><strong>My questions:</strong></p>
  
  <ol>
  <li>How can I both validate the XML document and access the value of the Elements?  Should I use another process to validate the XML against an XSD besides using XDocument.Validate? </li>
  <li>Is the problem in my Linq query?   </li>
  <li><p>When I try to specify a specific Element in the Linq query, I never get results.  It
  only works when it is</p>
  
  <pre><code>IEnumerable&lt;XElement&gt; residents =
          from xeRes in xD.Elements()
          select xeRes;
  </code></pre>
  
  <p>but</p>
  
  <pre><code>IEnumerable&lt;XElement&gt; residents =
          from xeRes in xD.Elements("Resident")
          select xeRes;
  </code></pre></li>
  </ol>
  
  <p>returns nothing.</p>
  
  <p>Any suggestions would be most welcome.</p>
  
  <p>Thank you,</p>
  
  <p>Code Snipets:</p>
  
  <p>XSD</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;  &lt;xsd:schema xmlns="http://kinduit.net/ResidentNS" xmlns:schema="http://kinduit.net/ResidentNS" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"&gt;&lt;xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" /&gt;
  &lt;xsd:element name="Resident"&gt;
      &lt;xsd:complexType&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="FacilityID"&gt;
                  &lt;xsd:simpleType&gt;
                      &lt;xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                          &lt;xsd:maxLength value="50" /&gt;
                      &lt;/xsd:restriction&gt;
                  &lt;/xsd:simpleType&gt;
              &lt;/xsd:element&gt;
              &lt;xsd:element name="ResidentID"  maxOccurs="unbounded"&gt;
                  &lt;xsd:simpleType&gt;
                      &lt;xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                          &lt;xsd:maxLength value="20" /&gt;
                      &lt;/xsd:restriction&gt;
                  &lt;/xsd:simpleType&gt;
              &lt;/xsd:element&gt;
              &lt;xsd:element name="ResidentID2"  maxOccurs="unbounded"&gt;
                  &lt;xsd:simpleType&gt;
                      &lt;xsd:restriction base="sqltypes:varchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52"&gt;
                          &lt;xsd:maxLength value="20" /&gt;
                      &lt;/xsd:restriction&gt;
                  &lt;/xsd:simpleType&gt;
              &lt;/xsd:element&gt;...
  </code></pre>
  
  <p>XML</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;&lt;Resident xmlns="http://kinduit.net/ResidentNS"&gt;
  &lt;FacilityID&gt;WARMSPRINGS&lt;/FacilityID&gt;
  &lt;ResidentID&gt;WS585459&lt;/ResidentID&gt;
  &lt;ResidentID2&gt;145214&lt;/ResidentID2&gt;...
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>        // Validate XML Schema...
          XmlSchemaSet sc = new XmlSchemaSet();
          XNamespace xNs = "http://kinduit.net/ResidentNS";
  
          try
          {
              // Validate against the XSD...
              string location = System.Reflection.Assembly.GetAssembly(typeof(ElementsBC.Interface)).Location;
              sc.Add(xNs.ToString(), location.Replace("ElementsBC.dll", "") + "\\InterfaceXSD\\resident.xsd");
              XDocument xD = this.ConvertToXDocument(ResidentXML);
              xD.Validate(sc, (sender, e) =&gt; { throw new Exception(e.Message); }, true);
  
              IEnumerable&lt;XElement&gt; residents =
                  from xeRes in xD.Elements()
                  select xeRes;
  
              counts[0] = residents.Count();
  
              foreach (XElement el in residents)
              {
                  try
                  {
                      // get facility...
                      string facilityid = el.Element("FacilityID").Value.ToString();
  </code></pre>
  
  <p>This last line returns null if there is a Namespace in the XML.  If there is no Namespace, this code runs lax Validation, but can read the value.</p>
  
  <p>Suggestions?</p>