Tuesday, December 22, 2009

XmlDataDocument - Synchronizing DataSet and XmlDocument

Introduction

XmlDataDocument object combines relational data model (DataSet) and hierarchical data model (XML document) and performs transparent synchronization between them to ensure that at any point of time, both models contain the same data.
This means that any change made to the DataSet is authomatically reflected in the corresponding XMLDocument.

XmlDataDocument Using Example

In order to demonstrate how to work with XmlDataDocument object, we will create a simple dataset (representing books database):


DataSet newDS = new DataSet("BooksDS");

DataTable authorsTable = new DataTable("Authors");
authorsTable.Columns.Add("AuthorID", typeof(int));
authorsTable.Columns.Add("FirstName", typeof(string));
authorsTable.Columns.Add("LastName", typeof(string));
authorsTable.PrimaryKey = new DataColumn [] { authorsTable.Columns["AuthorID"]};

authorsTable.Rows.Add(1, "Benjamin", "Franklin");
authorsTable.Rows.Add(2, "Herman", "Melville");

newDS.Tables.Add(authorsTable);

DataTable booksTable = new DataTable("Books");
booksTable.Columns.Add("BookID", typeof(int));
booksTable.Columns.Add("AuthorID", typeof(int));
booksTable.Columns.Add("BookTitle", typeof(string));
booksTable.Columns.Add("Genre", typeof(string));
booksTable.Columns.Add("PublicationDate", typeof(DateTime));
booksTable.Columns.Add("ISBN", typeof(string));
booksTable.PrimaryKey = new DataColumn[] { booksTable.Columns["BookID"] };

booksTable.Rows.Add(1, 1, "The Autobiography of Benjamin Franklin", "autobiography", DateTime.Now, "1-2-234234-4");
booksTable.Rows.Add(2, 2, "The Confidence Man", "novele", DateTime.Now, "5-2-234234-4");

newDS.Tables.Add(booksTable);

newDS.Relations.Add("AuthorsBooks",
newDS.Tables["Authors"].Columns["AuthorID"],
newDS.Tables["Books"].Columns["AuthorID"]);
newDS.Relations[0].Nested = true;



Now, we can create an instance of XmlDataDocument by feeding its constructor with newDS



XmlDataDocument xmlDoc = new XmlDataDocument(newDS);


Since XmlDataDocument inherits from XmlDocument, we have all XML-related objects within it.
In addition, XmlDataDocument contains DataSet object which references our newDS instance and allows access to relational view.

We can persist our XmlDataDocument into XML file and check how the inner XML looks like:



xmlDoc.Save("books.xml");


Fllowing XML file is generated:



<booksds>
<authors>
<authorid>1</authorid>
<firstname>Benjamin</firstname>
<lastname>Franklin</lastname>
<books>
<bookid>1</bookid>
<authorid>1</authorid>
<booktitle>The Autobiography of Benjamin Franklin</booktitle>
<genre>autobiography</genre>
<publicationdate>2009-12-22T12:15:06.7278538+02:00</publicationdate>
<isbn>1-2-234234-4</isbn>
</books>
</authors>
<authors>
<authorid>2</authorid>
<firstname>Herman</firstname>
<lastname>Melville</lastname>
<books>
<bookid>2</bookid>
<authorid>2</authorid>
<booktitle>The Confidence Man</booktitle>
<genre>novele</genre>
<publicationdate>2009-12-22T12:15:06.7278538+02:00</publicationdate>
<isbn>5-2-234234-4</isbn>
</books>
</authors>
</booksds>


Let's change some field value in newDS and save XML again:


newDS.Tables["Books"].Rows[0]["BookTitle"] = "The Revised biography of Benjamin Franklin";
xmlDoc.Save("books.xml");


As we can see, the change is automatically reflected in xmlDoc:



<booksds>
<authors>
<authorid>1</authorid>
<firstname>Benjamin</firstname>
<lastname>Franklin</lastname>
<books>
<bookid>1</bookid>
<authorid>1</authorid>
<booktitle><strong><span >The Revised</span> </strong>biography of Benjamin Franklin</booktitle>
<genre>autobiography</genre>
<publicationdate>2009-12-22T12:15:06.7278538+02:00</publicationdate>
<isbn>1-2-234234-4</isbn>
</books>
</authors>
<authors>
<authorid>2</authorid>
<firstname>Herman</firstname>
<lastname>Melville</lastname>
<books>
<bookid>2</bookid>
<authorid>2</authorid>
<booktitle>The Confidence Man</booktitle>
<genre>novele</genre>
<publicationdate>2009-12-22T12:15:06.7278538+02:00</publicationdate>
<isbn>5-2-234234-4</isbn>
</books>
</authors>
</booksds>


You can find the detailed info about this powerful object here:
http://msdn.microsoft.com/en-us/library/1t4362sd.aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmldatadocument.aspx

Thats' it,
Mark.

2 comments: