Solved

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

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

22 Experts available now in Live!

Get 1:1 Help Now