Solved

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

Posted on 2011-09-29
3
457 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now