0

Say I have the following XML schema:

<root>
   <version>2.0</version>
   <type>fiction</type>
   <chapters>
      <chapter>1</chapter>
      <title>blah blah</title>
   </chapter>
   <chapters>
      <chapter>2</chapter>
      <title>blah blah</title>
   </chapters>
</root>

Would it be possibly to parse the elements which I know will not be repeated in the XML and store them directly into the struct using LINQ?

For example, could I do something like this for "version" and "type"

//setup structs
Book book = new Book();
book.chapter = new Chapter();

//use LINQ to parse the xml
var bookData = from b in xmlDoc.Decendants("root")
               select new
               {
                   book.version = b.Element("version").Value,
                   book.type = b.Element("type").Value
               };

//then for "chapters" since I know there are multiple I can do:
var chapterData = from c in xmlDoc.Decendants("root"
                  select new
                  {
                    chapter = c.Element("chapters")   
                  };

foreach (var ch in chapterData)
{
    book.chapter.Add(getChapterData(ch.chapter));
}
Luke
  • 3
  • 1
  • 3
  • Please don't use mutable structs. – Mark Byers Jan 17 '11 at 18:22
  • Could you expand on why? I'm all for making my coding practices better. – Luke Jan 17 '11 at 18:25
  • 1
    For a basic rundown on evil mutable structs: http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil – Anthony Pegram Jan 17 '11 at 18:50
  • `struct`s inherit from `System.ValueType` and having them mutable can cause unexpected results such as demonstrated here http://www.java2s.com/Code/CSharp/Class-Interface/Calwayscreatesastructureinstanceasavaluetypevariableevenusingthenewoperator.htm – Davy8 Jan 17 '11 at 18:56

1 Answers1

2

Given your XML example, you should be able to parse the XML in a single spot. For the sake of this answer, let's assume your Book and Chapter are the following

class Book
{
    public string Version { get; set; }
    public string Type { get; set; }
    public List<Chapter> Chapters { get; set; }
}

class Chapter
{
    public int Number { get; set; }
    public string Title { get; set; }
}

Then to use the XML to create a Book object, we can do this

Book book = new Book
{
    Version = xmlDoc.Root.Element("version").Value,
    Type = xmlDoc.Root.Element("type").Value,
    Chapters = (from chapter in xmlDoc.Descendants("chapters")
                select new Chapter
                {
                    Number = (int)chapter.Element("chapter"),
                    Title = chapter.Element("title").Value
                }).ToList()
};

Note: I'm going with classes here because structs should be small and (preferrably) immutable, whereas your book and its chapters can be the latter but not likely to be the former. The fact that the object is encapsulating other non-structs is a dead giveaway to me that the object should also not be struct.

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
  • Thank you, this will give me some avenue to purse. Is there any nominal performance difference? I inherited the code. Currently it uses serialized struct and I'm just wondering if there was a reason. IE performance, or just didn't know any better. – Luke Jan 17 '11 at 19:29