SolvedPrivate

Parsing through XML C#

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
exchange ,script 10 44
Hey!!!! 1 22
Please explain the parts of these 2 LINQ expressions 3 31
Expression Evaluater 3 26
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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