Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
SolvedPrivate

Parsing through XML C#

Posted on 2014-11-06
5
Medium Priority
?
135 Views
Last Modified: 2016-02-18
I'm parsing through an XML document. There is  Parent "Q" and Child "G"

I am only interested in retrieving the parent data.

There are two problems

1. If there are no children, the parent is ignored and I don't see the results in the list
2. If there are children, the output writes the same row for the number of children. Should only be a single parent written

var results2 = (from System.Xml.Linq.XElement w in xmlDoc.Descendants("Q")

                                select new

                                {

                                    Qxml = new QUEUES

                                    {

                                       QID = (w.Attribute("id") != null) ? Convert.ToInt32(w.Attribute("id").Value) : 0,

                                       QNAME = (w.Attribute("n") != null) ? w.Attribute("n").Value : " ",

                                       QAS = (w.Attribute("as") != null) ? w.Attribute("as").Value : " ",

                                       QAT = (w.Attribute("at") != null) ? w.Attribute("at").Value : " ",

                                       QAI = (w.Attribute("ai") != null) ? w.Attribute("ai").Value : " ",

                                       QAP = (w.Attribute("ap") != null) ? w.Attribute("ap").Value : " ",

                                       QAW = (w.Attribute("aw") != null) ? w.Attribute("aw").Value : " "

                                   }

                              }).ToList();

 

                allQueues = results.Select(a => a.Qxml).ToList();

Open in new window

0
Comment
Question by:petel2k
[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
  • 3
5 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 total points
ID: 40426541
I tried your code. I get one QUEUES object per Q element. Nothing in your code depends on the presence of G elements, or their number.
0
 

Author Comment

by:petel2k
ID: 40426577
Thanks!!  Just realized I was using results2  however ->  allQueues = results.Select(a => a.Qxml).ToList();
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40426580
Hi petel2k;

Can you post a sample of your XML document please, thanks.
0
 

Author Closing Comment

by:petel2k
ID: 40426581
Thanks
0
 

Author Comment

by:petel2k
ID: 40426673
- <Q id="4224" n="Dnll" as="2280" at=" " ai=" " ap=" " aw=" ">

  <G id="3560" n="Kansas" />

  <G id="3561" n="Burlington" />

  <G id="3717" n="Hopkinton" />

  </Q>

  <Q id="4226" n="Ept" as=" " at=" " ai=" " ap=" " aw=" " />

- <Q id="4356" n="JJP" as=" " at=" " ai=" " ap=" " aw=" ">

  <G id="2230" n="NSP" />

  </Q>

not  getting parent when no children
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

670 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