Question

I had this xml :

<cats>
    <cat>
      <name>funycat_00</name>
      <category>funy</category>
      <link>/data/funy/funy_cat00</link>
    </cat>
    <cat>
      <name>funycat_01</name>
      <category>funy</category>
      <link>/data/funy/funy_cat01</link>
    </cat>
    <cat>
      <name>funycat_02</name>
      <category>funy</category>
      <link>/data/funy/funy_cat02</link>
    </cat>
    <cat>
      <name>funycat_03</name>
      <category>funy</category>
      <link>/data/funy/funy_cat03</link>
    </cat>
</cats>

My class :

        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        private string _picture;

        public string Picture
        {
            get { return _picture; }
            set { _picture = value; }
        }

        private string _category;
        public string Category
        {
            get { return _category; }
            set { _category = value; }
        }

        private List<Catpict> _listpict;
        public List<Catpict> Listpict
        {
            get { return _listpict;}
            set { _listpict = value; }
        }

        public List<Catpict> feed()
        {
            Catpict tempcat = new Catpict();

            var reader = XDocument.Load("Data/DataFile.xml");
            IEnumerable<XElement> cats = reader.Elements();

            foreach (var cat in cats)
            {
                tempcat.Title = cat.Element("Name").Value;
                Listpict.Add(tempcat);
            }
            return Listpict;
        }
    }
}

The problem is the parsing of my XML. I want to send into my collection Listpict all cat in my xml.

The question maybe stupid, and my english is realy bad.

If somebody have some answer for me i'm totally open.

Catpic is my class name.

Was it helpful?

Solution

I think you want something like this...

    public static List<CatPict> feed()
    {
        CatPict tempcat = new CatPict();
        string xml = XDocument.Load("XMLFile1.xml").ToString();

        using (XmlReader reader = new XmlTextReader(new StringReader(xml)))
        {
            while (reader.Read())
            {
                //put your logic here
            }
        }
        return Listpict;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top