• 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 guess we can agree to disagree. I had the completely opposite experience, and not just that it annoyed me, I was frequently unable to use the sites I visited regularly.
    • Yeah, so reliable, it kept failing to install via Windows Update with the following error on my i7 Surface Pro 7+, stating it's not even ready almost a year later:
    • Reminds me of the meme with Obama giving Obama a medal.
    • 😂😂😂😂. ok whatever you say MS.
    • Samsung rolls out next big UI update to Galaxy Watch Ultra by Devesh Beri Samsung has announced that its Galaxy Watch Ultra now receives One UI 8 Watch, which means that Galaxy Watch Ultra users now have access to new health tools designed to support daily wellness, including Running Coach, Vascular Load, and Antioxidant Index. The new interface on One UI 8 Watch has been made to give important information swiftly on the watch’s small screen. One can customize Multi-Info Tiles, which have health data, weather, and other metrics. The Now Bar, first introduced in One UI 7, keeps current actions and tasks always within reach. Samsung says the update brings more tools to improve sleep, heart health, fitness, and nutrition. The Galaxy Watch Ultra now features Bedtime Guidance, which provides personalized sleep times based on recent sleep trends, and the ability to measure vascular load during rest, allowing the watch to track the stress placed on the heart during sleep. These features, even though they are visible on the Samsung Health app for users of older watch models, can only be used by Watch8 users. It also comes with a Running Coach. The watch will analyze a 12-minute run to set a level and then give a training plan for goals like a marathon or a new distance. The Antioxidant Index measures the levels of skin carotenoids in seconds. It provides feedback on lifestyle choices that can help with healthy aging. Samsung says these updates are aimed at giving a more complete health experience, using instant feedback to help guide users’ choices. The Samsung Galaxy Watch8 was officially unveiled on July 9, 2025, at the Galaxy Unpacked event along with all-new Galaxy Z Fold7, Galaxy Z Flip7, and Galaxy Z Flip7 FE. This series consists of 3 different watches: the Watch8, Watch8 Classic (the one with rotating bezel), and Watch Ultra (2025). Source: Samsung
  • Recent Achievements

    • Week One Done
      SmileWorks Dental earned a badge
      Week One Done
    • Community Regular
      vZeroG went up a rank
      Community Regular
    • Collaborator
      Snake Doc earned a badge
      Collaborator
    • Week One Done
      Snake Doc earned a badge
      Week One Done
    • One Month Later
      Johnny Mrkvička earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      589
    2. 2
      Michael Scrip
      199
    3. 3
      ATLien_0
      195
    4. 4
      +FloatingFatMan
      133
    5. 5
      Xenon
      122
  • Tell a friend

    Love Neowin? Tell a friend!