Go Premium for a chance to win a PS4. Enter to Win

x
?
SolvedPrivate

Parsing through XML C#

Posted on 2014-11-06
5
Medium Priority
?
141 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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

971 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