Click here to Skip to main content
13,406,896 members (54,198 online)
Click here to Skip to main content
Add your own
alternative version


14 bookmarked
Posted 12 Dec 2011

Creating FlowDocuments using XML Literals and Embedded Expressions

, 14 Dec 2011
Rate this:
Please Sign up or sign in to vote.
How to create a FlowDocument using XML Literals and Embedded Expressions.


A UserControl displaying a FlowDocument created using XML literals


In this article I will show how you can go about defining a FlowDocument using XML literals and embedded expressions. As XML literals are exclusive to VB.NET this article may not be palatable to some die-hard C-Sharpers. Hopefully, in the near future, C# will also have the benefit of XML literals. If your programming curiosity gets the better of you then please read on.


A Flow Document is a document that has rich layout features. It can dynamically lay-out its content based on window size, device resolution, and other variables. Flow documents are mainly suitable for on-screen viewing and provide advanced document features like pagination, columns, search, viewing modes, and printing (search, viewing modes, and printing support are provided through the flow document containers).

The following XAML code creates a flow document, hosted by a FlowDocumentPageViewer, that displays some text:

    <Paragraph>CodeProject: Your Development Resource</Paragraph>

The result of the XAML code is:


You can read more on flow documents here.

XML Literals

An XML literal is XML code that is literally defined within your Visual Basic code. Using XML literals you can define XML documents and fragments directly within your code. The following example shows the creation of an XML document and XML element:

Dim cpDoc As XDocument = <?xml version="1.0"?>
Dim cpElm As XElement = <CodeProject>

Embedded Expressions

Embedded expressions work in concert with XML literals and enable the creation of dynamic XML documents or XML fragments. Using embedded expressions XML nodes can be created at runtime using values from variables or LINQ queries. This is done through the use of the embedded expression operator <%= %>.

Creating an XML document using some variable values is as easy as:

Dim firstName As String = "Meshack"
Dim lastName As String = "Musundi"

Dim cpDoc As XDocument = <?xml version="1.0"?>
                                 <FirstName><%= firstName %></FirstName>
                                 <LastName><%= lastName %></LastName>

Creating an xml element using a LINQ query is as easy as:

Dim names() As String = {"Bill", "Meshack", "Vee"}

Dim cpElm As XElement = <CodeProject>
                            <%= From n In names Select _
                                    <Name><%= n %></Name>
                                </Member> %>

The structure of the XML element generated above will be as follows:


XML Literals + Embedded Expressions = Dynamic FlowDocument

If you want to create a 'dynamic' flow document, one option would be to define the flow document programmatically. The other option would be to change portions of a flow document that is already defined in XAML. Using the latter approach, you would have to dig through the deeply nested content of the flow document in order to find the content you want to change. Unfortunately, both of these approaches are tedious and very unintuitive.

Using XML literals and embedded expressions, the process of creating a dynamic flow document can be more bearable. Consider that we intend to create a flow document whose final result will be as follows:


The data source for the flow document table will be a DataTable that is defined programmatically, to avoid having to create a database. The image displayed in the flow document will be from an application resource, Logo.png.


To create the DataTable, a method named AlbumTable() will create and return the required object.

Private Function AlbumTable() As DataTable
    Dim table As New DataTable

    table.Columns.Add("No.", GetType(Integer))
    table.Columns.Add("Album", GetType(String))
    table.Columns.Add("Artist", GetType(String))

    table.Rows.Add(1, "Let England Shake", "PJ Harvey")
    table.Rows.Add(2, "Bon Iver", "Bon Iver")
    table.Rows.Add(3, "21", "Adele")
    table.Rows.Add(4, "whokill", "tUnE-yArDs")
    table.Rows.Add(5, "Watch the Throne", "Jay-Z and Kanye West")
    table.Rows.Add(6, "The Whole Love", "Wilco")
    table.Rows.Add(7, "The English Riviera", "Metronomy")
    table.Rows.Add(8, "Skying", "The Horrors")
    table.Rows.Add(9, "House of Balloons", "The Weeknd")
    table.Rows.Add(10, "Helplessness Blues", "Fleet Foxes")

    Return (table)
End Function

We can then create an XML element, using an XML literal with the required embedded expressions, before converting it into a FlowDocument which is used to set the Document property of a FlowDocumentPageViewer.

Private Sub ShowReport(ByVal albumTable As DataTable)

    Dim doc = <FlowDocument xmlns=""
                  FontFamily="Myriad Pro" FontSize="14" ColumnWidth="594">
                  <BlockUIContainer><Image Source="Logo.png" Width="380" Height="78"/></BlockUIContainer>

                  <Paragraph Margin="10" FontWeight="SemiBold">
                      Top Albums of 2011 Report

                  <Table Margin="10" BorderThickness="1" BorderBrush="Gainsboro">
                          <TableColumn Width="*"/>
                          <TableColumn Width="5*"/>
                          <TableColumn Width="5*"/>

                          <TableRow Background="Crimson" Foreground="White" FontWeight="SemiBold">
                              <TableCell><Paragraph Margin="4">No.</Paragraph></TableCell>
                              <TableCell><Paragraph Margin="4">Album</Paragraph></TableCell>
                              <TableCell><Paragraph Margin="4">Artist</Paragraph></TableCell>

                          <!--Data rows-->
                          <%= From alb In albumTable.Rows Select _
                              <TableRow xmlns="">
                                  <TableCell BorderThickness="1" BorderBrush="Gainsboro">
                                      <Paragraph Margin="4"><%= alb(0) %></Paragraph>
                                  <TableCell BorderThickness="1" BorderBrush="Gainsboro">
                                      <Paragraph Foreground="MidnightBlue" Margin="4"><%= alb(1) %></Paragraph>
                                  <TableCell BorderThickness="1" BorderBrush="Gainsboro">
                                      <Paragraph Foreground="Gray" Margin="4"><%= alb(2) %></Paragraph>
                              </TableRow> %>

    Dim docString As String = doc.ToString()

    Dim flowDoc As FlowDocument = XamlReader.Load(New XmlTextReader(New StringReader(docString)))
    FlowDocPgVwr.Document = flowDoc

End Sub
Private Sub MainWindow_Loaded(ByVal sender As Object, _
                              ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
End Sub

One thing I noted with this approach is that you have to specify the XML namespace in the first element following the Select statement of the LINQ query. If you don't specify the namespace, a namespace will be specified automatically but with a null value. This will result in a XamlParseException when attempting to convert docString to XAML.

Another thing to take-note-of/remember is the fact that XAML is case-sensitive. If you forget to capitalize the element names appropriately, e.g., <Tablerow> instead of <TableRow>, a XamlParseException will be generated. This approach is also not WYSIWYG so you have to run the application to get a look at what exactly is the end result. Despite this, you can still get the required results if you have a good understanding of how to create flow documents.

Data Binding

From the example shown previously, it is notable that this approach creates the possibility of having a data-bindable like flow document. You could nest a flow document container in a UserControl and define a dependency property of the suitable data type. This is the approach used in the application whose screenshot is shown at the beginning of this article.

Public Property Table As DataTable
        Return GetValue(TableProperty)
    End Get

    Set(ByVal value As DataTable)
        SetValue(TableProperty, value)
    End Set
End Property

Public Shared ReadOnly TableProperty As DependencyProperty = _
                       DependencyProperty.Register("Table", _
                       GetType(DataTable), GetType(ReportViewer), _
                       New FrameworkPropertyMetadata(New PropertyChangedCallback( _
                                                              AddressOf TableChanged)))

Private Shared Sub TableChanged(ByVal sender As Object, _
        ByVal e As DependencyPropertyChangedEventArgs)
    Dim newTable As DataTable = CType(e.NewValue, DataTable)
    CType(sender, ReportViewer).ShowReport(newTable)
End Sub

If this approach doesn't seem suitable, you could opt to follow the approach described here.


That's it. I hope that you picked up something useful from this article.


  • 13th Dec., 2011: Initial post.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Meshack Musundi
Software Developer
Kenya Kenya
Meshack is a software developer with a passion for WPF.


  • CodeProject MVP 2013
  • CodeProject MVP 2012

You may also be interested in...


Comments and Discussions

QuestionCorrect me if I am wrong Pin
Sacha Barber12-Dec-11 22:42
mvpSacha Barber12-Dec-11 22:42 
AnswerRe: Correct me if I am wrong Pin
mfrasiu12-Dec-11 23:01
membermfrasiu12-Dec-11 23:01 
GeneralRe: Correct me if I am wrong Pin
Sacha Barber13-Dec-11 2:41
mvpSacha Barber13-Dec-11 2:41 
GeneralRe: Correct me if I am wrong Pin
Sacha Barber13-Dec-11 2:42
mvpSacha Barber13-Dec-11 2:42 
AnswerRe: Correct me if I am wrong Pin
Meshack Musundi12-Dec-11 23:37
memberMeshack Musundi12-Dec-11 23:37 
GeneralRe: Correct me if I am wrong Pin
Sacha Barber13-Dec-11 2:41
mvpSacha Barber13-Dec-11 2:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180221.1 | Last Updated 15 Dec 2011
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid