title: c# how to return xml results async from xmlReader
body: <p>I have recently started playing around with the async and await methods - I manged to read an HTML page and display my results in a grid. </p>
  
  <p>I am now aiming to do the same thing when reading an XML doc. When stepping through my code, it seems to work, but the results are not passed to the front end as and when results are found [even though it is getting results in the background]   </p>
  
  <p>After doing some research, I am suspecting that I might need something in the lines of:</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt; Load(xml));
  </code></pre>
  
  <p>Rather than my current:</p>
  
  <pre><code>while (await xmlReader.ReadAsync())
  </code></pre>
  
  <p>But I am not sure at all - alternatively, could it be that I am not returning the results in the correct fashion to be handled in time?</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>// To Update Frontend
      private async void button4_Click(object sender, EventArgs e)
      {
          var res = await doXml();
  
          foreach (var r in res)
          {
              l1_table.Rows.Add(r.url_, r.lastMod_, r.priority_);
          }
      }       
  
      //To read and Process XML Async
      public async Task&lt;List&lt;xmlLocs&gt;&gt; doXml()
      {
  
          List&lt;xmlLocs&gt; list = new List&lt;xmlLocs&gt;();
  
          bool isLoc = false;
          bool isLastMod = false;
          bool isPriority = false;
  
          string url = "";
          string lastMod = "";
          string priority = "";
  
          using (XmlReader xmlReader = XmlReader.Create("myXmlUrl", new XmlReaderSettings() { Async = true }))
          {
              while (await xmlReader.ReadAsync())
              {
                  switch (xmlReader.NodeType)
                  {
                      case XmlNodeType.Element:
                          switch (xmlReader.Name)
                          {
                              //check current element name
                              case "loc":
                                  isLoc = true;
                                  isLastMod = false;
                                  isPriority = false;
                                  break;
                              case "lastmod":
                                  isLoc = false;
                                  isLastMod = true;
                                  isPriority = false;
                                  break;
                              case "priority":
                                  isLoc = false;
                                  isLastMod = false;
                                  isPriority = true;
                                  break;
                              default:
                                  break;
                          }
                          break;
                      case XmlNodeType.Text:
                          //extract text value from element
                          if (isLoc)
                          {
                              url = xmlReader.Value;
                          }
                          if (isLastMod)
                          {
                              lastMod = xmlReader.Value;
                          }
                          if (isPriority)
                          {
                              //always last element in structure
                              priority = xmlReader.Value;       
  
                              //add result to list                        
                              xmlLocs lc = new xmlLocs() { url_ = url, lastMod_ = lastMod, priority_ = priority };
                              list.Add(lc);
  
                              //??? Maybe another way to return this ???
  
                          }
                          break;
                      default:
                          break;
                  }
              }
              //return list ?
              return list;
          }
      }
  </code></pre>
  
  <p>Xml tructure of the file I am reading:</p>
  
  <pre><code>&lt;url&gt;
      &lt;loc&gt;Location 123&lt;/loc&gt;
      &lt;lastmod&gt;2016-02-15&lt;/lastmod&gt;
      &lt;priority&gt;56&lt;/priority&gt;
  &lt;/url&gt;
  &lt;url&gt;
      &lt;loc&gt;Location 555&lt;/loc&gt;
      &lt;lastmod&gt;2016-11-22&lt;/lastmod&gt;
      &lt;priority&gt;80&lt;/priority&gt;
  &lt;/url&gt;
  </code></pre>
  
  <p>Thanks in advance
  C Bass</p>