JerryMungo
Honorary Master
- Joined
- Jul 18, 2008
- Messages
- 37,797
- Reaction score
- 6,466
.
Last edited:
South Africa’s biggest forum. Discuss, discover, and connect with thousands of members.
<?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);
}
?>
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.
Have you considered deserializing it to a list of Job objects matching a class structure loosely based on your XML schema?
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.That's a pretty good idea... I'll xsd it and see how that goes...
public class Job
{
public String Name;
public String SrvName;
public String AuthType;
public String Login;
public String Pass;
}
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();
foreach (Job job in jobs)
{
Console.WriteLine(job.Name + " " + job.SrvName + " " + job.AuthType + " " + job.Login + " " + job.Pass);
}
Did someone steal your teddy bear?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.
[)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); }
Happy to help.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

[)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
This!Have you considered deserializing it to a list of Job objects matching a class structure loosely based on your XML schema?
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.
This!
My daily work involves deserializing XML and doing all sorts with the results. It's the least complicated way forward.
[)roi(];18088524 said:As with most aspects of coding; it's down to personal preference / opinion.
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.
...and then throw in some XSLT and you can easily translate an object from one type to another:I was hoping for thisI need to update the XML too!