Welcome to MSDN Blogs Sign in | Join | Help

XMLSerializer, XDocument and LINQ

I took the sample from my previous post and wanted to use LINQ to query the XML. You can pretty much use the power of SQL on the document object to richly query for things you are looking for.

 

using System;

using System.Linq;

using System.Xml.Linq;

using System.Collections.Generic;

using System.Xml;

using System.Xml.Serialization;

using System.IO;

 

namespace Sample

{

    public class Company

    {

        List<Book> books;

 

        [XmlElement(ElementName = "Book")]

        public List<Book> Books

        {

            get

            {

                if (this.books == null)

                {

                    this.books = new List<Book>();

                }

                return books;

            }

            set { }

        }

    }

 

    public class Book

    {

        [XmlAttribute(AttributeName = "Title")]

        public string Name { get; set; }

 

        [XmlAttribute(AttributeName = "Author")]

        public string Author { get; set; }

 

        [XmlAttribute(AttributeName = "Year")]

        public string Year { get; set; }

    }

 

    public class Demo

    {

        static void Main(string[] args)

        {

            Company c = new Company

            {

                Books =

                {

                    new Book

                    {

                        Name = "First Book",

                        Author = "First Author",

                        Year = "First Year",

                    },

                    new Book

                    {

                        Name = "Second Book",

                        Author = "Second Author",

                        Year = "Second Year",

                    },

                    new Book

                    {

                        Name = "Third Book",

                        Author = "Third Author",

                        Year = "Third Year",

                    },

                    new Book

                    {

                        Name = "Fourth Book",

                        Author = "Fourth Author",

                        Year = "Fourth Year",

                    },

                    new Book

                    {

                        Name = "Fifth Book",

                        Author = "Fifth Author",

                        Year = "Fifth Year",

                    }

                }

            };

 

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Company));

            FileStream fs = new FileStream("test.xml", FileMode.Create);

            xmlSerializer.Serialize(fs, c);

            fs.Close();

            XDocument doc = XDocument.Load("test.xml");

           

            var bks = from books in doc.Elements("Company").Elements("Book") where (books.Attribute("Title").Value.Equals("Fourth Book")) select books;

            foreach (var book in bks)

            {

                Console.WriteLine(book);

            }

        }

    }

}

Published Tuesday, September 30, 2008 9:19 PM by thottams@microsoft.com

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# XMLSerializer, XDocument and LINQ : EasyCoded

Wednesday, October 01, 2008 12:36 AM by XMLSerializer, XDocument and LINQ : EasyCoded

# re: XMLSerializer, XDocument and LINQ

Better yet, serialize straight into XDocument:

var doc = new XDocument();

var (var writer = doc.CreateNavigator().AppendChild())

{

 xmlSerializer.Serialize(writer, c);

}

Wednesday, October 01, 2008 3:30 AM by int19h

# re: XMLSerializer, XDocument and LINQ

It is cool to query all objects with unified well known SQL language, but I feel a bit concerned about this dynamic SQL style rather than stored procedure.

Tuesday, October 07, 2008 8:56 AM by Bali

# re: XMLSerializer, XDocument and LINQ

From the investigation I have done there could be type bloating if not used carefully. Also, the object model is generated from the data model and changes when the data model changes if not crafted carefully. Having a normalized object model could lead to confusion in the application layer and could tie the implementation to LINQ only subsystems. Sure, careful design is required when modelling these systems.

Wednesday, October 08, 2008 1:34 PM by thottams@microsoft.com

# re: XMLSerializer, XDocument and LINQ

Tuesday, October 21, 2008 12:06 PM by Bali

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker