C# LINQ to XML help

If it was me and if it was in PHP I would have done something like this:

PHP:
<?php
$XML->load('http://your-xml-source/');
	$feed = array();
	foreach ($XML->getElementsByTagName('item') as $node) {
		$item = array ( 
			'Field_Lorim' => $node->getElementsByTagName('Some_Field_Name')->item(0)->nodeValue,
			'Field_Ipsum' => $node->getElementsByTagName('Another_Field_Name')->item(0)->nodeValue,
			'Field_Bacon' => $node->getElementsByTagName('More_Field_Name')->item(0)->nodeValue,
			'Field_Cheese' => $node->getElementsByTagName('Last_Field_Name')->item(0)->nodeValue,
			);
		array_push($feed, $item);
	}

?>

and then in the HTML I would do a little for loop and simply echo the fields into the areas that has to be populated.
 
If it was me and if it was in PHP I would have done something like this:

PHP:
<?php
$XML->load('http://your-xml-source/');
	$feed = array();
	foreach ($XML->getElementsByTagName('item') as $node) {
		$item = array ( 
			'Field_Lorim' => $node->getElementsByTagName('Some_Field_Name')->item(0)->nodeValue,
			'Field_Ipsum' => $node->getElementsByTagName('Another_Field_Name')->item(0)->nodeValue,
			'Field_Bacon' => $node->getElementsByTagName('More_Field_Name')->item(0)->nodeValue,
			'Field_Cheese' => $node->getElementsByTagName('Last_Field_Name')->item(0)->nodeValue,
			);
		array_push($feed, $item);
	}

?>

and then in the HTML I would do a little for loop and simply echo the fields into the areas that has to be populated.

Yeah, I may resort to loops, but trying to avoid them for efficiency - great when it's small but could get rather cumbersome. Unfortunately it's C# winforms too :)
 
Have you considered deserializing it to a list of Job objects matching a class structure loosely based on your XML schema?
 
That's a pretty good idea... I'll xsd it and see how that goes...
You don't need to. Having a schema validating your XML documents would be great, but it's not necessary. Just decorate your properties in the classes with XmlAttribute, XmlElement, XmlArray, etc depending on the structure of the document.
 
Create a backing class for Job, example:
Code:
public class Job
{
   public String Name;
   public String SrvName;
   public String AuthType;
   public String Login;
   public String Pass;
}

Then extract the xml into a collection of this class.
Code:
XDocument xdoc = XDocument.Load("filepath.xml");
List<Job> jobs = (from job in xdoc.Descendants("Job") select new Job { 
  Name = job.Attribute("Name").Value,
  SrvName = job.Attribute("SrvName").Value,
  AuthType = job.Attribute("AuthType").Value,
  Login = job.Attribute("Login").Value,
  Pass = job.Attribute("Pass").Value
}).ToList();

Finally, either iterate through the elements (below example), or link it up directly with your UI elements.
Code:
foreach (Job job in jobs)
{
   Console.WriteLine(job.Name + " " + job.SrvName + " " + job.AuthType + " " + job.Login + " " + job.Pass);
}
 
I'm gonna start keeping score of how many times posters come in and repost exactly what hat a previous guy said.

You sir are at 2 this week alone.
 
I'm gonna start keeping score of how many times posters come in and repost exactly what hat a previous guy said.

You sir are at 2 this week alone.
Did someone steal your teddy bear?:whistle::D
 
[)roi(];18087568 said:
Create a backing class for Job, example:
Code:
public class Job
{
   public String Name;
   public String SrvName;
   public String AuthType;
   public String Login;
   public String Pass;
}

Then extract the xml into a collection of this class.
Code:
XDocument xdoc = XDocument.Load("filepath.xml");
List<Job> jobs = (from job in xdoc.Descendants("Job") select new Job { 
  Name = job.Attribute("Name").Value,
  SrvName = job.Attribute("SrvName").Value,
  AuthType = job.Attribute("AuthType").Value,
  Login = job.Attribute("Login").Value,
  Pass = job.Attribute("Pass").Value
}).ToList();

Finally, either iterate through the elements (below example), or link it up directly with your UI elements.
Code:
foreach (Job job in jobs)
{
   Console.WriteLine(job.Name + " " + job.SrvName + " " + job.AuthType + " " + job.Login + " " + job.Pass);
}

Thanks - examples are always very helpful and welcome!

Hmm... It's only finding one instance of Job in the file - there are definitely 2 as per the example. It's also not finding the attributes for some reason...
*EDIT* never mind, awesome example is awesone, thanks droid! I had an issue with duplicate data files for some reason.


For some other reason I can't fathom, it's not seeing my System.Xml.Linq reference - I had to be explicit about the full reference to XDocument...
System.Xml.Linq.XDocument
 
Last edited:
Thanks - examples are always very helpful and welcome!

Hmm... It's only finding one instance of Job in the file - there are definitely 2 as per the example. It's also not finding the attributes for some reason...
*EDIT* never mind, awesome example is awesone, thanks droid! I had an issue with duplicate data files for some reason.


For some other reason I can't fathom, it's not seeing my System.Xml.Linq reference - I had to be explicit about the full reference to XDocument...
System.Xml.Linq.XDocument
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.
Screen Shot 2016-08-03 at 9.02.34 PM.png
 
[)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

Will check and post in the morning, ta muchly - it's helpful posts like this that make MyBB what it is.
Cheers!
 
This!
My daily work involves deserializing XML and doing all sorts with the results. It's the least complicated way forward.
As with most aspects of coding; it's down to personal preference / opinion.
 
This!
My daily work involves deserializing XML and doing all sorts with the results. It's the least complicated way forward.

Indeed, I've learned something valuable!

[)roi(];18088524 said:
As with most aspects of coding; it's down to personal preference / opinion.

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 :)
 
Last edited:
Can someone explain to me how deserializing works with an example in php?

I also want to learn.
 
Indeed, I've learned something valuable!



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 :)

Take note that once you have designed/decorated your classes to deserialize properly, you can serialize back to XML with almost zero extra effort.
 
Take note that once you have designed/decorated your classes to deserialize properly, you can serialize back to XML with almost zero extra effort.

I was hoping for this :) I need to update the XML too!
 
Top
Sign up to the MyBroadband newsletter
X