2013-05-02 3 views
1

Я получаю xml этого формата в ответ от веб-запроса, но я не могу связать его с данными, записанными в wpf.WPF bind datagrid с xml, возвращаемым в ответе веб-сервера

Вот мой XML:

<population> 
<name>Tram, Joshua </name> 
    <id>83804</id> 
    <hcp>Dr. Krueger</hcp> 
    <symptoms>4</symptoms> 
    <range>1/17/13 - 4/13/13</range> 
    <last7>5</last7> 
    <connect>41380</connect> 
    <engage>5</engage> 
    <daysin>160</daysin> 
    <education>0.67</education> 
    <recco>Encourage Education</recco> 
    <name>Riess, Chuck </name> 
    <id>73403</id> 
    <hcp>Dr. Vockell</hcp> 
    <symptoms>4</symptoms> 
    <range>2/1/13 - 2/14/13</range> 
    <last7>5</last7> 
    <connect>41332</connect> 
    <engage>5</engage> 
    <daysin>179</daysin> 
    <education>0.74</education> 
    <recco>Encourage Tracking</recco> 
    <name>Park, Teruyuki </name> 
    <id>69235</id> 
    <hcp>Dr. Smithen</hcp> 
    <symptoms>3</symptoms> 
    <range>4/3/13 - 4/13/13</range> 
    <last7>5</last7> 
    <connect>41384</connect> 
    <engage>5</engage> 
    <daysin>35</daysin> 
    <education>0.15</education> 
    <recco> </recco> 
    </population> 

и вот мой код, я храню его в наборе данных и связывания с DataGrid, но он показывает только на колонке не все.

WebRequest request = HttpWebRequest.Create(url); 
WebResponse response = request.GetResponse();   

DataSet set = new DataSet();    

set.ReadXml(response.GetResponseStream());    

return set; 
+0

XML-кажется странно, если не сказать ... во всяком случае, что вы пробовали? Вы спрашиваете о привязке к datagrid WPF, но предоставленный вами код не показывает строки, связанные с WPF ... – RoelF

+0

Похоже, что в xml отсутствуют теги. Существует несколько экземпляров каждого тега (то есть , , и т. Д.), Как если бы они были обернуты другим тегом. – Murkaeus

+0

нет его фактического xml, возвращаемого в ответ webrequest –

ответ

1

я решил мою проблему итерации на элементах в XML и создание списка объекты класса таким образом:

public List<Population> GetData() 
    { 
     DataSet ds =null; 

     List<Population> populationList = new List<Population>(); 

     string url = "http://www.lyfechannel.com/channels/WIN946/get_population.php"; 

     WebRequest request = HttpWebRequest.Create(url); 

     WebResponse response = request.GetResponse(); 

     StreamReader reader = new StreamReader(response.GetResponseStream()); 

     XmlDocument doc = new XmlDocument(); 

     doc.LoadXml(reader.ReadToEnd()); 


     XmlNode rootNode = doc.SelectSingleNode("//population"); 

     XmlNodeList currentNodes = doc.SelectNodes("//name"); 

     var objPopulation = new Population[currentNodes.Count]; 

     if (currentNodes.Count != 0) 
     { 

      for (int i = 0; i < currentNodes.Count; i++) 
      { 
       objPopulation[i] = new Population(); 
       //objPopulation[i].Name = currentNodes[i].InnerText; 

      } 

     } 


     XmlNodeList idNodeList = doc.SelectNodes("//id"); 

     if (idNodeList.Count != 0) 
     { 

      for (int i = 0; i < idNodeList.Count; i++) 
      { 
       objPopulation[i] = new Population(); 
       objPopulation[i].ID = idNodeList[i].InnerText; 


       currentNodes = doc.SelectNodes("//name"); 


       if (currentNodes.Count != 0) 
       { 

        for (int ij = 0; ij < currentNodes.Count; ij++) 
        { 

         objPopulation[ij].Name = currentNodes[ij].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//hcp"); 


       if (currentNodes.Count != 0) 
       { 

        for (int j = 0; j < currentNodes.Count; j++) 
        { 

         objPopulation[j].HCP = currentNodes[j].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//symptoms"); 


       if (currentNodes.Count != 0) 
       { 

        for (int k = 0; k < currentNodes.Count; k++) 
        { 

         objPopulation[k].Symptoms = currentNodes[k].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//range"); 


       if (currentNodes.Count != 0) 
       { 

        for (int l = 0; l < currentNodes.Count; l++) 
        { 

         objPopulation[l].Range = currentNodes[l].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//last7"); 


       if (currentNodes.Count != 0) 
       { 

        for (int m = 0; m < currentNodes.Count; m++) 
        { 

         objPopulation[m].Last7 = currentNodes[m].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//connect"); 


       if (currentNodes.Count != 0) 
       { 

        for (int n = 0; n < currentNodes.Count; n++) 
        { 

         objPopulation[n].Connect = currentNodes[n].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//engage"); 


       if (currentNodes.Count != 0) 
       { 

        for (int o = 0; o < currentNodes.Count; o++) 
        { 

         objPopulation[o].Engage = currentNodes[o].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//daysin"); 


       if (currentNodes.Count != 0) 
       { 

        for (int p = 0; p < currentNodes.Count; p++) 
        { 

         objPopulation[p].DaysIn = currentNodes[p].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//education"); 


       if (currentNodes.Count != 0) 
       { 

        for (int q = 0; q < currentNodes.Count; q++) 
        { 

         objPopulation[q].Education = currentNodes[q].InnerText; 

        } 

       } 


       currentNodes = doc.SelectNodes("//recco"); 


       if (currentNodes.Count != 0) 
       { 

        for (int r = 0; r < currentNodes.Count; r++) 
        { 

         objPopulation[r].Recco = currentNodes[r].InnerText; 

        } 

       } 

       populationList.Add(objPopulation[i]); 

      } 

     } 

     MyGrid.ItemsSource = populationList; 


    } 

и вот мой код класса:

class Population 
{ 

    public string Name { get; set; } 
    public string ID { get; set; } 
    public string HCP { get; set; } 
    public string Symptoms { get; set; } 
    public string Range { get; set; } 
    public string Last7 { get; set; } 
    public string Connect { get; set; } 
    public string Engage { get; set; } 
    public string DaysIn { get; set; } 
    public string Education { get; set; } 
    public string Recco { get; set; } 

}