1

I have an Xml document:

<?xml version="1.0" encoding="utf-8"?>
<Family xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Person member="father" id="0">
    <Surname>Smith</Surname>
    <Forename>Robert</Forename>
      <Person member="son" id="1">
        <Surname>Smith</Surname>
        <Forename>Sam</Forename>
          <Person member="son" id="2">
            <Surname>Smith</Surname>
            <Forename>Jeff</Forename>
          </Person>
      </Person>
      <Person member="daughter" id="3">
        <Surname>Smith</Surname>
        <Forename>Sarah</Forename>
      </Person>
  </Person>
</Family>

...and a few supporting classes as follows:

   [XmlRoot]
   public class Family {

      [XmlElement]
      public List<Person> Person;
   }

   public class Person {

      [XmlAttribute("member")]
      public MemberType Member { get; set; }

      [XmlAttribute("id")]
      public int Id { get; set; }

      [XmlElement]
      public string Surname { get; set; }

      [XmlElement]
      public string Forename { get; set; }

      [XmlElement("Person")]
      public List<Person> People;
   }

   public enum MemberType {
      Father,
      Mother,
      Son,
      Daughter
   }

Now, say Family has a method defined as such:

public IEnumerable<Person> Find (Func<Person, bool> predicate) {
    //  also, I know that this SelectMany will not work - assume this foreach works
    //  on a flattened list of people
    foreach (var p in family.Person.SelectMany()) {
        if(predicate(p)) {
            yield return p;
        }
    }
}

...but I don't want to deserialize the Xml to the Family and Person classes. I would like to simply load the XDocument and query that directly - but working with XElement, XAttribute, and XName is not that friendly when providing an API. I realize that I need the classes - Family & Person - but they are simply models.

Can I have a Find method where I can pass something like:

IEnumerable<Person> people = someBusinessObj.Find(p => p.Forename == "Jeff");

Update
I would prefer a solution that does not involve an open-source project (as @MartinHonnen refers).

IAbstract
  • 19,551
  • 15
  • 98
  • 146

1 Answers1

1

Why do you not want to deserialize the XML into objects? This will give you exactly the programmatic interface you require. I would:

  1. Deserialize using the XmlSerializer class.
  2. Allow you users to query via Linq-to-Objects.

All this requires very little effort to implement!

ColinE
  • 68,894
  • 15
  • 164
  • 232
  • The idea was to use XDocument and write the Xml. Querying with Linq to Xml is not eloquent - i.e. not typed and full of additional query language not everyone is used to. If Linq to Xsd were more mature and part of .Net (not an open-source add on library), it would be an easier matter to resolve. At this point we will continue with the plan to serialize and deserialize the Xml. But, I am still looking for an answer to the question as it is. :) – IAbstract Jan 16 '12 at 15:07