Solved

Using linq to get data out of an XML...

Posted on 2011-09-29
3
459 Views
Last Modified: 2013-11-11
I have an xDocument that I am trying to get data out of.  I need to get two elements:
packageid
expectedweight

I am trying to use linq and I am having a hard time.  I want to loop through each package and grab these values and add them to my list of objects.

Here is what I have so far.
lstContainers = new List<EntityContainer>();

            var xmlParse = XElement.Parse(xmlDoc.ToString());

            var packages = from p in xmlDoc.Descendants()
                         where p.Name.LocalName == "package"
                         select p;

            XNamespace xns = xmlParse.Name.Namespace;

            if (packages != null && packages.Count() > 0)
            {
                foreach (var p in packages)
                {
                    EntityContainer objContainer = new EntityContainer();

                    objContainer.strContainerID = xmlParse.Descendants(xns + "packageid").Single().Value;
                    objContainer.dblTotalWeight = Convert.ToDouble(xmlParse.Descendants(xns + "expectedweight").Single().Value);

                    lstContainers.Add(objContainer);
                }
            }

Open in new window

xmlTest.xml
0
Comment
Question by:Bran-Damage
  • 2
3 Comments
 

Author Comment

by:Bran-Damage
ID: 36818248
I figured out what I was doing wrong.  in the foreach i was still trying to use xmlParse.  I need to replace that with p...

The last question I have with this is this the best way to do what I am trying to do with linq.  Seems like a lot of code to get it.
0
 
LVL 11

Accepted Solution

by:
brutaldev earned 500 total points
ID: 36818268
Ignore the name space stuff, your XML doesn't contain any of that. You can also avoid the LINQ query because the Descendants has an overload for the name. I would also avoid the parse in the beginning, you can access the elements directly off the package XElement object. The count is also overkill, if it's not null then you can go straight to the loop.

Something like this (untested):
 
lstContainers = new List<EntityContainer>();

var packages = xmlDoc.Descendants("package");

if (packages != null)
{
  foreach (var p in packages)
  {
    EntityContainer objContainer = new EntityContainer();

    objContainer.strContainerID = p.Element("packageid").Value;
    objContainer.dblTotalWeight = Convert.ToDouble(p.Element("expectedweight").Value);

    lstContainers.Add(objContainer);
  }
}

Open in new window

0
 

Author Closing Comment

by:Bran-Damage
ID: 36818343
Cool, thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question