• 0

Parse XML with LINQ not reading values


Question

Hi, we're in the process of rewriting one of our webapps to .NET 4.5 and we are trying to use LINQ to parse our XML for outputting. I have the following XML and C# below, but it writes out nothing, I have a feeling it maybe something simple I've overlooked in the code, any help/guidance on LINQ would be appreciated.
 

XML

<?xml version="1.0" encoding="utf-8"?>
<Items>
  <Status>OK</Status>
  <ListOf>
    <Item>
      <Name>Test 1</Name>
      <Description>Test of a really long description</Description>
      <Picture>http://test.com/test.jpg</Picture>
      <Price>Free</Price>
    </Item>
    <Item>
      <Name>Test 2</Name>
      <Description>Test of a really long description</Description>
      <Picture>http://test.com/test2.jpg</Picture>
      <Price>Free under 5s</Price>
    </Item>
  </ListOf>	
</Items>

C#

System.Collections.Specialized.NameValueCollection _query = new System.Collections.Specialized.NameValueCollection() {
{"mode", "test"}
};

using (var wclient = new WebClient())
{
	var result = wclient.UploadValues("http://{XML_ADDRESS_REMOVED}", _query);
	string xml = System.Text.Encoding.UTF8.GetString(result);

	XDocument doc = XDocument.Parse(xml);
	var data = from item in doc.Descendants("Item")
			   select new
			   {
				   Name = item.Element("Name").Value.ToString(),
			   };

	foreach (var item in data)
	{
		Response.Write("Title: " + item.Name + "<br />");
	}
}

2 answers to this question

Recommended Posts

  • 0

I tested the "LINQ" part of the code and it works fine using the provided example XML data. Step into the code and make sure you're getting the correct response from the server.

 

Code used to test:

private void button1_Click(object sender, EventArgs e)
        {
            XDocument doc = XDocument.Parse(File.ReadAllText("data.xml"));
 
            var data = from item in doc.Descendants("Item")
                        select new
                        {
                            Name = item.Element("Name").Value.ToString(),
                        };
 
            foreach (var item in data)
            {
                listBox1.Items.Add("Title: " + item.Name + "<br />");
            }
        }

Output in listBox1:

Title: Test 1<br />
Title: Test 2<br />
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I don't get why this company needs to change sockets every 2 generations, I really don't. If AMD can deliver a superior product both in terms of performance and energy consumption on the same socket for multiple generations, why can't Intel. I don't like to wear the tinfoil hat, but come on, this stinks of backroom deals with motherboard manufacturers? New socket, new motherboards, more sales.
    • unlimited internet, not unlimited data lol sure you can go anywhere on there but it's gonna cost you to do it!
    • Intel's next-gen desktop processors leak with a whopping number of cores by Taras Buria Intel's third-generation Core Ultra processors for desktop computers are on the way. If the latest leak is true, we are up for some significant updates in the Core Ultra 300 Series. @g01d3nm4ng0 on X posted a list of Intel's upcoming desktop processors, which includes seven SKUs ranging from entry-level Core Ultra 3 models to the flagship Core Ultra 9 with a rather whopping number of cores. According to the leak, the top-end model will feature 52 cores without hyperthreading. However, those cores are not like "classic" cores found in HEDT chips like AMD's Threadrippers. Instead, those 52 cores are divided into three categories: performance, efficiency, and low-power. The upcoming Core Ultra 9 reportedly features 16 performance cores, 32 efficiency cores, and 4 low-power cores. The cheapest Core Ultra 3 has four of each, 12 cores in total. Low-power cores are new to Intel's desktop processors. The current Core Ultra 200 chips feature the same hybrid configuration that was introduced with the 12th-gen Alder Lake chips: performance cores for demanding tasks and efficiency cores for lightweight tasks (up to 24 cores). Now, Intel wants to improve the efficiency of its chips further by adding another set of low-power cores, which were first introduced in the first-gen Core Ultra mobile chips (Meteor Lake). @jaykihn0 adds that Intel Nova Lake-S will support 8000 MT/s memory speed by default and 32 PCIe Gen 5 lanes and 16 PCIe Gen 4 lanes, totalling 48 lanes in CPU + chipset configuration. SKU P Cores E Cores LP Cores TDP Core Ultra 9 16 32 4 150W Core Ultra 7 14 24 4 150W Core Ultra 5 8 16 4 125W Core Ultra 5 8 12 4 125W Core Ultra 5 6 8 4 125W Core Ultra 3 4 8 4 65W Core Ultra 3 4 4 4 65W Today's leak aligns with previous reports about the upcoming Intel Nova Lake-S processors. However, Intel enthusiasts should probably get ready to switch platforms again, as the Nova Lake-S generation is rumored to require a brand-new LGA1954 socket.
  • Recent Achievements

    • Explorer
      treker_ed went up a rank
      Explorer
    • Apprentice
      CHUNWEI went up a rank
      Apprentice
    • Veteran
      1337ish went up a rank
      Veteran
    • Rookie
      john.al went up a rank
      Rookie
    • Week One Done
      patrickft456 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      656
    2. 2
      ATLien_0
      272
    3. 3
      +FloatingFatMan
      176
    4. 4
      Michael Scrip
      157
    5. 5
      Steven P.
      136
  • Tell a friend

    Love Neowin? Tell a friend!