Hello, I have an issue I've been beating my head on a little. I have a list that I've made from an xml document and I'm trying to call that list and print it to console and run queries on it. So far nothing I've tried or searched and implemented has worked yet. I'm sure it's something simple I'm missing, maybe you could lend a hand? I've deleted what I tried, it was a mess.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXml
{
    class LinqToXml
    {
        static void Main(string[] args)
        {

            Console.ReadKey();
        }


    }

    public class Plant
    {
        public string CommonName { get; set; }
        public string Zone { get; set; }


    }
    public class PlantHelper
    {
        public static List<Plant> GetPlants()
        {
            XDocument doc = XmlHelper.GetPlantDocument();
            var xmlPlants = doc.Descendants("PLANT");
            List<Plant> plants = new List<Plant>();

            foreach (var p in xmlPlants)
            {
                Plant plant = new Plant();
                plant.CommonName = p.Element("COMMON").Value;
                plant.Zone = p.Element("ZONE").Value;
                plants.Add(plant);
            }
            return plants;
        }
    }
}

Mind telling us exactly where the error is? Like what line is throwing it or the message or what?

I feel you only gave us part of the code since I never see you calling PlantHelper.GetPlants()

I had PlantHelper.GetPlants() above my console.readkey, and there were no errors thrown. just a blank console page. my only other code is my xmlhelper:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Xml;
using System.Xml.Linq;
namespace LinqToXml
{
    class XmlHelper
    {
        public static XDocument GetPlantDocument()
        {
            string appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;

            FileInfo asm = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

            //string appPath = Path.GetDirectoryName(SystemApplication.ExecutablePath);
            FileInfo fi = new FileInfo(asm.DirectoryName + @"\Data\PlantCatalog.xml");

            XDocument doc = XDocument.Load(fi.FullName);
            return doc;
        }
    }
}

Edited 3 Years Ago by kiail

which i don't think my error is in my xmlhelper class, but maybe. i just know i'm not calling my list right, i feel like PlantHelper.GetPlants() should have more to it, mainly because it's not writing anything to the console. I tried to create a stringbuilder also, which I almost forgot about. I thought that might do it.

public override string ToString()
        {
            return string.Format("PN: {0} \t Zone: {1}",
                                    CommonName, Zone);
        }

and this threw errors saying those names don't exist in the current context.

Edited 3 Years Ago by kiail

Well let me ask you this, are you running it in Visual Studio? If so place a breakpoint (click the far left, should be a strip left of all the + and - for collpasing code, you'll see a red dot appear and the line will be highlighted).

Then simply press F5 to run it. When you get to that breakpoint, go down to the window on the bottom left, the Autos tab, and look for your List (it might be under the "this" grouping).

There you can examine the variables, make sure you are look for the list you pass back to (you can see if it contains values, determining if it indeed has data in the first place to print).

Yes I'm seeing data in my plants list. The data looks proper.

I should also add, if you don't care to do this. Simply put a print statement after your foreach loop in PlantHelper. Just have it print a item from the first index, don't make it all fancy

This is good, at least I know my xmlhelper is working properly and i'm inserting my items in my list properly also. it's just calling it from that list i'm having issues with. I'm really having a dumb moment here, I've been programming all day and I haven't taken a break, bad headache, can't concentrate. I even forgot about break points(omg). It's going to be something as stupid as a console.writeline and I'll slap myself for this when I figure it out.

Edited 3 Years Ago by kiail

LOL so it sounds like maybe you should ditch that custom ToString () for now and just do something like

Console.WriteLine("PN: " + MyList[i].CommonName + "\t" + "Zone: " + MyList[i].Zone);

And trust me I know about those moments, usually that means you need to take a break, go eat something (you feeling weak or shaky?) and drink, maybe watch some TV on the coach

Hey thanks for helping me figure out where my problem is. i don't have it quite working, but i think i can figure it out after i take your advice of eating and a break. i tried just dropping that code in my main, but i'm going to have to figure out how to access my plants list to do it. anyway, thank you a bunch for your patience and help.

Yeah I figured it out. It was something simple too. Lol.

This question has already been answered. Start a new discussion instead.