C# LINQ to XML help

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
100% - and I'm glad I learned about deserializing XML. Seems way more logical and simple than just working with LINQ queries alone. Workign with XML just went from daunting to not too bad in one easy step :)
Linq is .NET's foray into Functional Programming, so it certainly will look more complicated against XmlSerializer type annotation; practically however they fulfil quite different needs.

Linq is the powerhouse, for example:
  • Effeciently processing large xml streams, whilst keeping memory allocations small.
  • Writing generic routines to process different data sets (I've cut out a lot of boilerplate with this.)
  • Lazy evaluation and deferred execution.
  • Large scale parallel queries across multiple clusters (https://www.microsoft.com/en-us/research/project/dryadlinq/)
...the cost for this is naturally complexity, but a lot of it can be offset during refactoring i.e. you don't need to have large Linq code blocks; most can be simplified to a line or two by offsetting sections to functions.
 
Last edited:

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
[)roi(];18090890 said:
Linq is .NET's foray into Functional Programming, so it certainly will look more complicated against XmlSerializer type annotation; practically however they fulfil quite different needs.

Linq is the powerhouse, for example:
  • Effeciently processing large xml streams, whilst keeping memory allocations small.
  • Writing generic routines to process different data sets (I've cut out a lot of boilerplate with this.)
  • Lazy evaluation and deferred execution.
  • Large scale parallel queries across multiple clusters (https://www.microsoft.com/en-us/research/project/dryadlinq/)
...the cost for this is naturally complexity, but a lot of it can be offset during refactoring i.e. you don't need to have large Linq code blocks; most can be simplified to a line or two by offsetting sections to functions.
I'm not dissing LINQ at all, I'm actually quite a fan of it and use it (and lambdas and Func delegates) extensively in just about everything I do. I just believe in horses for courses and, well, serialization annotations fit the bill quite nicely in this case.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I'm not dissing LINQ at all, I'm actually quite a fan of it and use it (and lambdas and Func delegates) extensively in just about everything I do. I just believe in horses for courses and, well, serialization annotations fit the bill quite nicely in this case.
Sorry, didn't mean for it to come across as a "this vs. that" competition.
I was just trying to cover OP's starting question re "I'm learning LINQ to XML...". i.e. framing that there is a place for Linq. Btw, I used to lean heavily on Xmlserialization, until FP; now most boiler plate irks me; yet in some cases it's more personal choice than absolute.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
[)roi(];18093078 said:
Sorry, didn't mean for it to come across as a "this vs. that" competition.
I was just trying to cover OP's starting question re "I'm learning LINQ to XML...". i.e. framing that there is a place for Linq. Btw, I used to lean heavily on Xmlserialization, until FP; now most boiler plate irks me; yet in some cases it's more personal choice than absolute.
I try to avoid XML like the plague whenever I can though. JSON is just much more elegant, readable and lightweight. XML and schemas still have a place in strongly typed data contracts and *puke* SOAP though...

But I agree, FP is a nice change of programming pace. When and where I can't apply FP I often tend to engineer the solutions in such a way to keep the work interesting for myself, which usually ends in a framework of sorts for my junior/mid-level devs to build against to ensure consistency, clean code and high performance. I become EXTREMELY unproductive when I have to copy-paste code and regurgitate the same work more than once.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I try to avoid XML like the plague whenever I can though. JSON is just much more elegant, readable and lightweight. XML and schemas still have a place in strongly typed data contracts and *puke* SOAP though...

But I agree, FP is a nice change of programming pace. When and where I can't apply FP I often tend to engineer the solutions in such a way to keep the work interesting for myself, which usually ends in a framework of sorts for my junior/mid-level devs to build against to ensure consistency, clean code and high performance. I become EXTREMELY unproductive when I have to copy-paste code and regurgitate the same work more than once.
Lol... sounds familiar...ME:D
 

Willie Trombone

Honorary Master
Joined
Jul 18, 2008
Messages
60,038
[)roi(];18087856 said:
Happy to help.

As comparison I have the following includes:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

If you still experiencing issue, just check that you have the relevant references loaded.
View attachment 379668

I made a silly error - I had the references loaded but not the includes in the form.cs so that's sorted, thanks!

What i'm trying to fathom now is the quickest way to get the Field names for Table1 and Table2 for a specified Job into a list...
I'm assuming a for each Job loop is the way to go and check the unique Job name, or is there a quicker way?
 
Last edited:

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I made a silly error - I had the references loaded but not the includes in the form.cs so that's sorted, thanks!

What i'm trying to fathom now is the quickest way to get the Field names for Table1 and Table2 for a specified Job into a list...
I'm assuming a for each Job loop is the way to go and check the unique Job name, or is there a quicker way?
Here's a rough example in Linq. Remember that you can achieve the same result with Xmlserialization; also if you're planning to continue with Linq; I'll suggest you find a good book on it.

Code:
public static class LINQExtension
{
  // Flatten IEnumerables; similar to flatmap in Haskell, Scala, ...
  public static IEnumerable<A> flatMap<A>(this IEnumerable<IEnumerable<A>> a)
  {
    return a.SelectMany(value => value);
  }
}

internal class ExtractFields
{
  // Extract Attributes for the Table(x) elements
  internal static IEnumerable<XAttribute> FilterTables(XElement element)
  {
    return (from elem in element.Descendants()
            where elem.Name.ToString().Contains("Table")
            select elem.Descendants().Attributes()).flatMap();
  }

  // Load XML, extract Job matching provided name, then Filter Table(x) and 
  // return Attributes of their Descendant Elements.
  internal static IEnumerable<XAttribute> LoadXml(String xmlPath, String jobName, String tag)
  {
    XDocument xdoc = XDocument.Load(xmlPath);
    return (from element in xdoc.Descendants(tag)
            where element.Attribute("Name").Value.Equals(jobName)
            select FilterTables(element)).flatMap();
  }

  // foreach loop to LoadXMl and iterate through Fields
  internal static void Print()
  {
    foreach (XAttribute field in LoadXml("myserver.xml", "Job1", "Job"))
    {
      // Note: we can access parental values if required, it helps to differentiate 
      //       between fields for Table1 vs. Table2)
      var table = field.Parent.Parent;
      var tableAttribute = table.Attribute("IDField");

      Console.WriteLine(table.Name + " / (" +
             tableAttribute.Name + " = " + tableAttribute.Value + ") / " +
             field.Parent.Name + " " + field.Name + " = " + field.Value + ")");
    }
  }
}
The output looks like this; naturally you'll adjust to fit your needs.
Code:
Table1 / (IDField = IDFieldName) / Field name = F1_Name)
Table1 / (IDField = IDFieldName) / Field name = F2_Name)
Table1 / (IDField = IDFieldName) / Field name = F3_Name)
Table2 / (IDField = IDFieldName) / Field name = F1_Name)
Table2 / (IDField = IDFieldName) / Field name = F2_Name)
Table2 / (IDField = IDFieldName) / Field name = F3_Name)
 

Willie Trombone

Honorary Master
Joined
Jul 18, 2008
Messages
60,038
I will take your advice and get a book on LINQ, though I suspect serialization would be simpler for what I'm trying to achieve here. Either way, thanks a ton for posting samples - incredibly helpful!
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
I will take your advice and get a book on LINQ, though I suspect serialization would be simpler for what I'm trying to achieve here. Either way, thanks a ton for posting samples - incredibly helpful!

Whew I made that way more difficult than it needed to be; guess that what comes from burning the late night oil.

This one should be easier to understand
Code:
XDocument xdoc = XDocument.Load("myfile.xml");
var job1 = from element in xdoc.Descendants("Job")
           where element.Attribute("Name").Value.Equals("Job1")
           select element;

var tables = from element in job1.Descendants() 
             where element.Name.ToString().Contains("Table") 
             select element;

var fields = from element in tables.Descendants().Attributes()
             select new { value = element.Value, 
                          name = element.Parent.Name, 
                          parent = element.Parent.Parent.Name };

foreach (var x in fields)
{
   Console.WriteLine(x.parent + " " + x.name + " " + x.value);
}

Here's the output from this:
Code:
Table1 Field F1_Name
Table1 Field F2_Name
Table1 Field F3_Name
Table2 Field F1_Name
Table2 Field F2_Name
Table2 Field F3_Name

Btw in regards to the book; here's an oldie (but goodie):
Essential Linq
 
Last edited:

Willie Trombone

Honorary Master
Joined
Jul 18, 2008
Messages
60,038
[)roi(];18098572 said:
Whew I made that way more difficult than it needed to be; guess that what comes from burning the late night oil.

This one should be easier to understand
Code:
XDocument xdoc = XDocument.Load("myfile.xml");
var job1 = from element in xdoc.Descendants("Job")
           where element.Attribute("Name").Value.Equals("Job1")
           select element;

var tables = from element in job1.Descendants() 
             where element.Name.ToString().Contains("Table") 
             select element;

var fields = from element in tables.Descendants().Attributes()
             select new { value = element.Value, 
                          name = element.Parent.Name, 
                          parent = element.Parent.Parent.Name };

foreach (var x in fields)
{
   Console.WriteLine(x.parent + " " + x.name + " " + x.value);
}

Here's the output from this:
Code:
Table1 Field F1_Name
Table1 Field F2_Name
Table1 Field F3_Name
Table2 Field F1_Name
Table2 Field F2_Name
Table2 Field F3_Name

Btw in regards to the book; here's an oldie (but goodie):
Essential Linq

Awesome! Thanks - getting back to the VS interface shortly.
What UI do you code in mostly btw?
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Awesome! Thanks - getting back to the VS interface shortly.
What UI do you code in mostly btw?
I'm on a Mac, so Visual Studio isn't natively available (I run it occasionally in a VM; usually only for testing)

As to the rest I tend to jump between a few IDEs:
If you're on Windows; then Visual Studio is always going to be one of the better options.
 

FarligOpptreden

Executive Member
Joined
Mar 5, 2007
Messages
5,396
[)roi(];18100452 said:
I'm on a Mac, so Visual Studio isn't natively available (I run it occasionally in a VM; usually only for testing)

As to the rest I tend to jump between a few IDEs:
If you're on Windows; then Visual Studio is always going to be one of the better options.
MonoDevelop isn't all that bad on OSX. Granted, I've only used it for a bit of C# work with Unity... But it felt lightweight and code completion felt robust enough.

Regarding Android Studio, it's based on IntelliJ from JetBrains. For Java development I've always favoured NetBeans. It's incredibly efficient having multiple projects open in a single IDE instance and being able to jump betqeen their codebases... Maven support feels unmatched as well.

I'm also waiting for Visual Studio Code to flesh out a bit. It feels a bit TOO spartan for my liking at the moment.
 

Willie Trombone

Honorary Master
Joined
Jul 18, 2008
Messages
60,038
[)roi(];18100452 said:
I'm on a Mac, so Visual Studio isn't natively available (I run it occasionally in a VM; usually only for testing)

As to the rest I tend to jump between a few IDEs:
If you're on Windows; then Visual Studio is always going to be one of the better options.

Yeah, a bit late to the party, but .Net Core is definitely gaining ground. VS Code is not a bad option. I'm using and enjoying Sublime Text as well for ad-hoc stuff.
 
Top