473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Readng XML via LINQ

Samishii23
246 100+
Due to a recent suggestion...

And after failing to make a simple program to play with the code and see what it does... I come bearing questions, and seeking help...

Console App
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using System.Xml.Linq;
  6. using System.Text;
  7.  
  8. namespace ConsoleApplication1
  9. {
  10.         class Program
  11.         {
  12.                 static void Main(string[] args)
  13.                 {
  14.                         var xdata = from d in XElement.Load("dk.xml").Elements("Talent")
  15.                                                 select d;
  16.  
  17.                         foreach (var data in xdata)
  18.                         {
  19.                                 Console.WriteLine(data);
  20.                         }
  21.                         Console.ReadLine();
  22.                 }
  23.         }
  24. }
Program compiles. All the console window does is the ReadLine() method, it doesn't show any data at all. can anyone tell me why I'm not getting any output?
Feb 3 '10 #1
12 4184
tlhintoq
3,525 Expert 2GB
Just guessing that the path "dk.xml" does not lead to a file.
I think the default search is to check in the same folder as the executable.
Perhaps testing with the file a known valid location like "C:\\dk.xml"
Feb 3 '10 #2
Samishii23
246 100+
Thanks for the quick response.

I tried putting the XML file in the C:/ . Same result.
I tried using Console.Write instead.
I tried using data.ToString().

All the "Talent" tags in my XML file with don't have a closing tag (The syntax is w3 compliant though). So I tried to read a tag with a closing tag... Still no go...

Just figured I would try and see if it did anything. Though it didn't seem to work. =\
Feb 3 '10 #3
Curtis Rutland
3,256 Expert 2GB
I've been meaning to do a writeup of this eventually, but I haven't had time. I'll do a quick condensed version here for you to get started. I really love LINQ and want everyone to start using it =D

First, make sure to add a reference to System.Xml.Linq if you haven't, and make sure it's in your using statements. (I see that you already have, but I'm putting this here for other people's benefit).

The first part of the following code gets all the descendant nodes of the root element in the XML document, then loops through them and prints their values. I used the standard long notation for this.

The second part shows a query to find a specific string in the xml, namely the value of the first descendant node called "body". I used shorthand and a lambda expression for this example.

Here's a sample that can get you started.
C# Program
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Linq;
  4.  
  5. namespace TermsHelper
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             XDocument doc = XDocument.Load(@"c:\dev\temp.xml");
  12.  
  13.             //standard notation
  14.             var data = from x in doc.Root.Descendants()
  15.                        select x;
  16.             foreach (XElement e in data)
  17.                 Console.WriteLine(e.Value);
  18.  
  19.             //alternate notation
  20.             string body = doc.Root.Descendants().Where(x => x.Name == "body").First().Value;
  21.  
  22.             Console.WriteLine("\nBody: {0}", body);
  23.  
  24.             //attribute example
  25.             string attribute = doc.Root.Descendants().Where(x => x.Name == "body").First().Attribute("type").Value;
  26.             Console.WriteLine("\nAttribute: {0}", attribute);
  27.             Console.Read();
  28.         }
  29.     }
  30. }
temp.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <note>
  3.     <to>Tove</to>
  4.     <from>Jani</from>
  5.     <heading>Reminder</heading>
  6.     <body type="text">Don't forget me this weekend!</body>
  7. </note>
Expected output when executed
Expand|Select|Wrap|Line Numbers
  1. Tove
  2. Jani
  3. Reminder
  4. Don't forget me this weekend!
  5.  
  6. Body: Don't forget me this weekend!
  7.  
  8. Attribute: text
Good luck.
Feb 3 '10 #4
Samishii23
246 100+
Don't suppose I could get a quickie example pertaining to attributes?
Feb 3 '10 #5
Curtis Rutland
3,256 Expert 2GB
Sure, I'll edit my original post to include an attribute example.

Attribute("name") is a method of XElement that returns the first XAttribute of the XElement that matches the name. Attributes() will return a list.
Feb 3 '10 #6
Samishii23
246 100+
Awesome. I believe I've got the hang of singular Linq'n here. lol
This is a quickie layout of my current XML file.
Expand|Select|Wrap|Line Numbers
  1. <base>
  2.   <tree1>
  3.     <tag1 />
  4.     ...
  5.     <tag44 />
  6.   </tree1>
  7.   ...
  8.   <tree3 />
  9. </base>
  10.  
So far I've wrote this:
Expand|Select|Wrap|Line Numbers
  1. var query = from x in xmldoc.Root.Descendants()
  2.     select (XElement)x.Element("Talent");
And when I console it. I get Tag1 from each of the 3 trees and then a $hit ton of White Space. And thats it... lol

What I want to do is basicly read each Tag into a List or Array. All the examples I can find half the time either just give me a single item return, or they create an XML which I will not be doing at all.
Feb 3 '10 #7
Curtis Rutland
3,256 Expert 2GB
This would work better:
Expand|Select|Wrap|Line Numbers
  1. var query = from x in xmldoc.Root.Descendants().Elements("Talent")
  2.     select x;
Here's an example that uses your sample XML and does what you want in two different ways. The first way just gets all the elements who's name contains the string "tag". The second uses a custom data type and nested LINQ queries to give you a list of trees, with each tree containing a list of tags.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Linq;
  3. using System.Xml.Linq;
  4. using System.Collections.Generic;
  5.  
  6. namespace TermsHelper
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             XDocument doc = XDocument.Load(@"c:\dev\temp.xml");
  13.  
  14.             //just tags
  15.             List<XElement> elements = (from x in doc.Root.Descendants()
  16.                                       where x.Name.LocalName.Contains("tag")
  17.                                       select x).ToList();
  18.  
  19.             foreach (XElement e in elements)
  20.                 Console.WriteLine(e.Name);
  21.  
  22.             Console.WriteLine();
  23.  
  24.             //custom data type, hierarchical
  25.  
  26.             List<Tree> trees = (from x in doc.Root.Descendants()
  27.                                 where x.Name.LocalName.Contains("tree")
  28.                                 select new Tree
  29.                                 {
  30.                                     TreeName = x.Name.ToString(),
  31.                                     Tags = (from y in x.Elements()
  32.                                             where y.Name.LocalName.Contains("tag")
  33.                                             select y).ToList()
  34.                                 }).ToList();
  35.  
  36.             foreach (Tree t in trees)
  37.             {
  38.                 Console.WriteLine("Tree Name: {0}", t.TreeName);
  39.                 Console.WriteLine("Tags:");
  40.                 foreach (XElement e in t.Tags)
  41.                     Console.WriteLine(e.Name);
  42.                 Console.WriteLine();
  43.             }
  44.  
  45.             Console.Read();
  46.         }
  47.     }
  48.     public class Tree
  49.     {
  50.         public string TreeName { get; set; }
  51.         public List<XElement> Tags { get; set; }
  52.     }
  53. }
Contents of temp.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2.  <base>
  3.    <tree1>
  4.      <tag1 />
  5.      <tag2 />
  6.      <tag44 />
  7.    </tree1>
  8.    <tree2>
  9.     <tag1 />
  10.    </tree2>
  11.    <tree3 />
  12. </base>
Expected Output
Expand|Select|Wrap|Line Numbers
  1. tag1
  2. tag2
  3. tag44
  4. tag1
  5.  
  6. Tree Name: tree1
  7. Tags:
  8. tag1
  9. tag2
  10. tag44
  11.  
  12. Tree Name: tree2
  13. Tags:
  14. tag1
  15.  
  16. Tree Name: tree3
  17. Tags:
  18.  
Feb 3 '10 #8
Samishii23
246 100+
Well. Thanks for all the help! Though this LINQ stuff is a headache, and I doubt I'll be learning much more since just trying to figure out your examples, I couldn't really figure it out. Most tutorials out there do more XML generation rather then reading. Especially the MSDN examples... Ugh.

This is what I came up with...
Expand|Select|Wrap|Line Numbers
  1. XDocument xmldoc = XDocument.Load("dk.xml");
  2.     var query = from x in xmldoc.Root.Descendants()
  3.             where x.Name == "Talent"
  4.             select x;
  5.     foreach (XElement q in query)
  6.     {
  7.         Console.Write(q.Name + ": ");
  8.         Console.Write(q.Attribute("id").Value + ",");
  9.         if (Int32.Parse(q.Attribute("pts").Value) > 0)
  10.         {
  11.             Console.Write(q.Attribute("pts").Value + ",");
  12.             Console.Write(q.Attribute("title").Value + ",");
  13.             Console.Write(q.Attribute("icon").Value);
  14.             if (q.LastAttribute.Name == "req")
  15.                 Console.Write("," + q.Attribute("req").Value);
  16.         }
  17.         else
  18.         {
  19.             Console.Write(q.Attribute("pts"));
  20.         }
  21.     Console.WriteLine("--");
Output is:
Expand|Select|Wrap|Line Numbers
  1. Tag: 1,5,title,icon
  2. Tag: 2,0
  3. ... (132 total)
Each "Tag" output will be listed in its own List.
Feb 12 '10 #9
Samishii23
246 100+
So ok. Now my next question... Using the above code... How would I get a sibling node ( if thats what its called? ) through the same loop?

Would I use another Decendant() call?
Expand|Select|Wrap|Line Numbers
  1. <talent>
  2.   <disc>
  3.     Some stuff here
  4.   </disc>
  5. </talent>
Feb 12 '10 #10
Curtis Rutland
3,256 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. q.Element["whatever"].Value
Should give you the value of the first child node named "whatever"
Feb 12 '10 #11
Samishii23
246 100+
The " [] " brackets didn't work, but the " () " did. Thanks!
Feb 13 '10 #12
Curtis Rutland
3,256 Expert 2GB
Oops, sorry, forgot it was a method and not an index.
Feb 13 '10 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dave Johnson | last post by:
Greetings, i want to be able to use linq new technology with sql server. the senario i am not able to do so far is as follow: 1- i program with linq 2- be able to generate and manipulate...
7
by: Chris | last post by:
I am a little confused. I have been reading about LINQ and it seems to imply LINQ is available in C# 3 but not in Visual Studio until the next release. I am a VB.net programmer but would still like...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
9
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
Hi all: after reading different places/sites about linq... I ran into these questions: 1. What framework do we need to run linq ? (does it depend on what version of visual studio we have?) how...
0
by: =?Utf-8?B?SHlwZXJjb2Rlcg==?= | last post by:
I'm encountering some strange behavior after deploying a ASP.net 3.5 website to production, i'm unable to reproduce these in my dev environment. This error seems to occur very randomly but it's...
4
by: =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post by:
We’re storing our main entity in an insert only table which stores the history of past revisions, but we’re facing problems with storing this history as LINQ will only update the entity, and...
14
by: thj | last post by:
Hi, I was wondering what you guys are using and why? LINQ to SQL or NHibernate? Thanks in advance, Tommy
3
by: =?Utf-8?B?UGF1bCBQcmV3ZXR0?= | last post by:
I'm attempting to use LINQ to insert a record into a child table and I'm receiving a "Specified cast is not valid" error that has something to do w/ the keys involved. The stack trace is: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.