title: C#: Parse XML line by line
body: <p>I am trying to parse an XML Document and convert it to a C# object. I succeeded with a few entries but for others I don't understand why it is not working. Here is the example xml:  </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"&gt;
      &lt;cbc:UBLVersionID&gt;2.1&lt;/cbc:UBLVersionID&gt;
      &lt;cbc:CustomizationID&gt;abcd&lt;/cbc:CustomizationID&gt;
      &lt;cbc:ProfileID&gt;urn:www.abc.eu:profile:bii05:ver2.0&lt;/cbc:ProfileID&gt;
      &lt;cbc:ID&gt;TOSL108&lt;/cbc:ID&gt;
      &lt;cbc:IssueDate&gt;2013-06-30&lt;/cbc:IssueDate&gt;
      &lt;cbc:InvoiceTypeCode listID="UL122001"&gt;38021&lt;/cbc:InvoiceTypeCode&gt;
      &lt;cbc:Note&gt;Ordered in our booth at the convention.&lt;/cbc:Note&gt;
      &lt;cbc:TaxPointDate&gt;2013-06-30&lt;/cbc:TaxPointDate&gt;
      &lt;cbc:DocumentCurrencyCode listID="ISO4217"&gt;NOK&lt;/cbc:DocumentCurrencyCode&gt;
      &lt;cbc:AccountingCost&gt;Project cost code 123&lt;/cbc:AccountingCost&gt;
      &lt;cac:InvoicePeriod&gt;
          &lt;cbc:StartDate&gt;2013-06-01&lt;/cbc:StartDate&gt;
          &lt;cbc:EndDate&gt;2013-06-30&lt;/cbc:EndDate&gt;
      &lt;/cac:InvoicePeriod&gt;
  &lt;/Invoice&gt;  
  </code></pre>
  
  <p>I succeeded at getting all the cbc items but not with cac. Here is the c# code:  </p>
  
  <pre><code>XmlDocument document = new XmlDocument();
  document.LoadXml(model.XmlDocument);
  
  XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);
  mgr.AddNamespace("cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
  mgr.AddNamespace("cbc", "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2");
  XmlElement docElement = document.DocumentElement;
  var a = docElement.SelectSingleNode("cbc:InvoiceTypeCode/@listID", mgr)?.InnerText;
  var b = docElement.SelectSingleNode("cbc:TaxCurrencyCode/@listID", mgr)?.InnerText;
  var invoice = new Invoice()
  {
      UBLVersionId = docElement.SelectSingleNode("cbc:UBLVersionID", mgr)?.InnerText,
      CustomizationID = docElement.SelectSingleNode("cbc:CustomizationID", mgr)?.InnerText,
      ProfileID = docElement.SelectSingleNode("cbc:ProfileID", mgr)?.InnerText,
      InvoiceId = docElement.SelectSingleNode("cbc:InvoiceId", mgr)?.InnerText,
      IssueDate = DateTime.Parse(docElement.SelectSingleNode("cbc:IssueDate", mgr)?.InnerText),
      InvoiceTypeCode = docElement.SelectSingleNode("cbc:InvoiceTypeCode", mgr)?.InnerText,
      InvoiceTypeCode_ListID = docElement.SelectSingleNode("cbc:InvoiceTypeCode/@listID", mgr)?.InnerText,
      TaxPointDate = docElement.SelectSingleNode("cbc:TaxPointDate", mgr)?.InnerText,
      DocumentCurrencyCode = docElement.SelectSingleNode("cbc:DocumentCurrencyCode", mgr)?.InnerText,
      DocumentCurrencyCodeListID = docElement.SelectSingleNode("cbc:DocumentCurrencyCode/@listID", mgr)?.InnerText,
      TaxCurrencyCode = docElement.SelectSingleNode("cbc:TaxCurrencyCode", mgr)?.InnerText,
      TaxCurrencyCodeListID = docElement.SelectSingleNode("cbc:TaxCurrencyCode/@listID", mgr)?.InnerText,
      AccountingCost = docElement.SelectSingleNode("cbc:AccountingCost", mgr)?.InnerText,
      InvoicePeriod = new InvoicePeriod()
      {
          StartDate = DateTime.Parse(docElement.SelectSingleNode("cac:InvoicePeriod/cbc:StartDate", mgr)?.InnerText),
          EndDate = DateTime.Parse(docElement.SelectSingleNode("cac:InvoicePeriod/cbc:EndDate", mgr)?.InnerText)
      }
  };  
  </code></pre>
  
  <ol>
  <li>Why is the InvoicePeriod parsing not working?  </li>
  <li>Is there a more efficient way to parse a document like this?</li>
  </ol>