views:

45

answers:

3

Given some sample XML such as:

<XML>  
  <EMPLOYEES>    
    <EMPLOYEE isBestEmployee="false">John"<"/EMPLOYEE>  
    <EMPLOYEE isBestEmployee="true">Joe"<"/EMPLOYEE>  
    <EMPLOYEE isBestEmployee="false">Bill"<"/EMPLOYEE>  
  </EMPLOYEES>  
</XML>  

How do I serialise just the employee with isBestEmployee="true" to a single Employee object?

A: 

I have two answers for this question, here's the first answer:

http://www.csharp-examples.net/xml-nodes-by-attribute-value/

drachenstern
A: 

I have two answers for this question, here's the second answer:

Given any list, how do you find a specific value?

You just have to parse the list.


Now, I'll assume you're not sure how to do that:

[XmlType("EMPLOYEES"), Serializable]
public class Employees {
   public Employee[] employee {get; set;}
}
[XmlType("EMPLOYEE")]
public class Employee {
   [XmlAttribute("isBestEmployee")]
   public bool bestEmployee {get; set;}
   [XmlText]
   public string name;
}

You should deserialize this (probably with XmlSerializer) and then you can parse the array using foreach (Employee in Employees) or a LINQ query or any of a number of other ways.

Does this answer the question?

drachenstern
Thanks. Your 2nd solution still requires me to serialise all the data into the list and then extract the "best employee". Is there a way to use XPath for serialising objects?
guazz
@guazz - try reading the first solution link. the second was just about how to declare the classes.
drachenstern
A: 

Are you referring to something like this (maybe have linq do the heavy lifting)?

 XDocument loaded = XDocument.Load(@"C:\YourXmlFile.xml"); //or xml in memory

         // Query the data and create the employee objects
        var q = from c in loaded.Descendants("EMPLOYEE")
                where (bool)c.Attribute("isBestEmployee") == true //"true"
                select new Employee() { Name = c.Value, isBestEmployee = (bool)c.Attribute("isBestEmployee") };


        //print out the list of employees if you want. 
        foreach (Employee e in q)
        {
           Console.WriteLine("Employee name = {0}, isBestEmployee = {1}", e.Name, e.isBestEmployee);
        }
AGoodDisplayName
The XPath is still less code...
drachenstern
True the example has less code, but like @guazz said he still has to loop to create the employees. With your exmaple he would have to create a list/collection of somekind and add the new employee(s) in the loop so that he could finally do soemthing with real objects...then it wouldn't be less code. Either way is not much work is required...just offering up options.
AGoodDisplayName
@AGoodDisplayName ~ Good points. What I'm scratching my head on is what does the business logic look like that says "I'll never ever ever need the EMPLOYEE info outside of knowing who's the best employee." and why the functor feeding this dataset can't restrict to the original business logic. There's a reason the data is being provided. Hence I provided two solutions. One to do what he asked (which I think he didn't read, since he asked about xpaths still) and one to do what the displayed data showed would later be possible. IDK, no requirements presented, yeah?
drachenstern
@drachenstern - agreed :-).
AGoodDisplayName