Change 1
Remember that we kept the XML chunk of
data of our previous sample in the CDATA section inline with the XAML code.
Using the MethodName=”Parse” specification in the Object DataProvider,
this XML data is parsed and stored as an XElement instance.
Now, let us take a look at the
resources section of the new XAML markup.
<ObjectDataProvider x:Key="LoadedBooks" ObjectType="{x:Type xlinq:XElement}" MethodName="Load" >
<ObjectDataProvider.MethodParameters>
<system:String>d:\bala\mydata.xml </system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
In the above markup, using the
specification MethodName=”Load” and by specifying the location(path) of
the XML file, XElement instance is created.
Change 2
The XML data is stored in mydata.xml
file. This XML data itself is different from what we have used inline in the
previous sample. It has a list of books with an attribute “id” and an element
“title”
<books xmlns="">
<book id="1">
<title>Welcome,
Home!</title>
</book>
<book id="2">
<title>Star
Letters</title>
</book>
<book id="3">
<title>Pink and
Blue</title>
</book>
<book id="4">
<title>Pirates of the Indian
Ocean</title>
</book>
</books>
Change 3
To reflect these changes in data
binding aspect of the WPF controls, there is a sligher change in the markup as
well.
Instead of
Text="{Binding
Path=Value}"
Now, we need to
write Text="{Binding
Path=Element[title].Value}"
Change 4
Similary, while adding a new book in
the existing list, do the following changes in the code for the handler
“OnAddBook”:
XElement newBook = new XElement(mybooks + "book",
new XAttribute("id", tbAddID.Text));
newBook.Add(new XElement("title", tbAddValue.Text));
bookList.LastNode.AddAfterSelf(" ", newBook, "\r\n");
lbBooks.Items.Refresh();
Upon doing the above mentioned changes,
run the sample code(Window2.xaml) and see the output as below:
|