C# problem reading out XML using HttpRequest

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
While reading an XML document generated by a web API, the data isnt been displayed. Also the LINQ output is empty.
This is the XML file generated by the web API:
<pre class="prettyprint <ArrayOfList xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WebAPI.Models

<List>
<ID>1</ID>
<Onderwerp>Gizmo 1</Onderwerp>
<tekst>hey</tekst>
</List>

<List>
<ID>2</ID>
<Onderwerp>Gizmo 2</Onderwerp>
<tekst>test</tekst>
</List>

<List>
<ID>3</ID>
<Onderwerp>Gizmo 3</Onderwerp>
<tekst>hallo</tekst>
</List>

</ArrayOfList>[/code]
<br/>
The code used for displaying the elements is the following:
<pre class="prettyprint using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Net;

namespace Things2DoDesktop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



private void Form1_Load(object sender, EventArgs e)
{
string urixml = "http://localhost:50621/api/lists/";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urixml);
request.KeepAlive = false;
request.Method = "GET";
request.ContentType = "text/xml";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Stream recieve = response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

StreamReader read = new StreamReader(recieve, encode);

string doc = read.ReadToEnd();

XDocument xmldoc = XDocument.Parse(doc);



var Lists = from List in xmldoc.Descendants("List")
select new
{
ID = List.Element("ID").Value,
Onderwerp = List.Element("Onderwerp").Value,
text = List.Element("text").Value
};
foreach (var List in Lists)
{
listBox1.Items.Add(List.ID.ToString());
listBox2.Items.Add(List.Onderwerp.ToString());
listBox3.Items.Add(List.text.ToString());
}

read.Close();
response.Close();

}
}
}
[/code]
<br/>
Why is the genereted LINQ empty and why arent there any items showed in the listboxs ?
Greets,
Emilie

View the full article
 
Back
Top