Solved

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

Posted on 2011-09-29
3
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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