SolvedPrivate

Parsing through XML C#

Posted on 2014-11-06
5
125 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 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 62

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 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

13 Experts available now in Live!

Get 1:1 Help Now