0

I have a xml structure like this. Can anyone help with a simple linq function to read this xml structure.The itemEntry node repeats according to data. I tried to read the xml using the method below,but i am getting no records in the list. Is this method here correct way to get the details...

List<CX_ITEMLIST> sList =
    (from e in XDocument.Load(param.FileName).Root.Elements("itemEntry")
     select new CX_ITEMLIST
     {
         TITLE = (string)e.Element("title"),
         YEAR = (string)e.Element("year"),
         ITEMNAME = (string)e.Element("itemname"),
         CATRYLIST =
         (
             from p in e.Elements("categorylist").Elements("categories")
             select new CATLIST
             {
                 IDTYPE = (string)p.Element("categoryid"),
                 IDNUMBER = (string)p.Element("categoryName")
             }).ToList()
     }).ToList();
<itemslist>
 <itemInformation>
  <itemdate>01/23/2014</itemdate>
  <itemcount>57</itemcount>
 </itemInformation>
 <itemEntry>
  <title>Title1</title>
  <year>2013</title>
  <itemname>testname</itemname>
  <categorylist>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  </categorylist>
 </itemEntry>
 <itemEntry>
  <title>Title1</title>
  <year>2013</title>
  <itemname>testname</itemname>
  <categorylist>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  <categories>
   <categoryid>Category1</categoryid>
   <categoryName>Category2</categoryName>
  </categories>
  </categorylist>
 </itemEntry>
</itemslist>
deepu
  • 1,993
  • 6
  • 42
  • 64
  • http://stackoverflow.com/a/670569/440030 – Reza ArabQaeni Feb 06 '14 at 06:42
  • I still find it far better to serialize Xml in to type-safe Pocos and then use those to operate on rather than bothering with manipulating Xml. – Moo-Juice Feb 06 '14 at 07:24
  • @RezaArab : i have updated the question with the linq method used please check. – deepu Feb 06 '14 at 07:24
  • "It's not working" is not working for me. Tell us what the problem is. – John Saunders Feb 06 '14 at 07:26
  • OP, since your update to your question, I just tried your new code and it works for me. Note that you have 2013 in 2 places and if you have that in your XML it's possible the error is being ignored and you get an empty list. I fixed the XML and your code as is in the question now with XDocument works perfectly. – Dmitriy Khaykin Feb 06 '14 at 15:08

3 Answers3

1

You should try with XDocument.

XDocument xdoc = XDocument.Load("file.xml");
John Saunders
  • 160,644
  • 26
  • 247
  • 397
mit
  • 1,763
  • 4
  • 16
  • 27
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). – John Saunders Feb 06 '14 at 07:27
0

The System.Xml.XLinq namespace contains some awesome objects to make this easy.

var xDoc = XDocument.Parse(xml); // load your xml string, or use XDocument.Load() to load an xml file

var itemEntries = xDoc
    .Root                       // refers to itemEntries node
    .Descendants("itemEntry");  // gets all itemEntry nodes in an IEnumerable object 

This gets you an IEnumerable<XNode> of all the itemEntry nodes.

From there you can do what you need, save the values to a business object, etc.

Dmitriy Khaykin
  • 5,238
  • 1
  • 20
  • 32
0

The above method works properly, i found the issue, my xml tag was having namespace attribute. i tried to get the namespace and append it with Elements while reading

XNamespace ns = xDocument.Root.Attribute("xmlns").Value;

List<CX_ITEMLIST> sList =
    (from e in XDocument.Load(param.FileName).Root.Elements(ns + "itemEntry")
     select new CX_ITEMLIST
     {
         TITLE = (string)e.Element(ns + "title"),
         YEAR = (string)e.Element(ns + "year"),
         ITEMNAME = (string)e.Element(ns + "itemname"),
         CATRYLIST =
         (
             from p in e.Elements(ns + "categorylist").Elements(ns + "categories")
             select new CATLIST
             {
                 IDTYPE = (string)p.Element(ns + "categoryid"),
                 IDNUMBER = (string)p.Element(ns + "categoryName")
             }).ToList()
     }).ToList();
deepu
  • 1,993
  • 6
  • 42
  • 64