SolvedPrivate

Parsing through XML C#

Posted on 2014-11-06
5
130 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 500 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 63

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Put File 6 36
How do i create a simple array in a public class? 6 36
Convert a string to date 4 40
msiexec won't run 4 25
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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