// Populate XElement contacts with Contacts information.
XElement contacts =
new XElement("Contacts",
new XElement("Contact1",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State", "WA"),
new XElement("Postal", "68042")
)
),
new XElement("Contact2",
new XElement("Name", "Yoshi Latime"),
new XElement("Phone", "503-555-6874"),
new XElement("Address",
new XElement("Street1", "City Center Plaza 516 Main St."),
new XElement("City", "Elgin"),
new XElement("State", "OR"),
new XElement("Postal", "97827")
)
)
);
// Create the first TextBlock control.
// Note that the element has to declare two XAML namespaces.
XElement textBlock1 = XElement.Parse(
@"<TextBlock
xmlns='http://schemas.microsoft.com/client/2007'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
TextWrapping= 'Wrap'
Width = '400'
Canvas.Top = '10'
Text=''/>");
// Get the first child element from contacts xml tree.
XElement contact1 = contacts.Element("Contact1");
// Set the value of the last attribute "Text"
// to the content of contacts xml tree.
textBlock1.LastAttribute.SetValue(contact1.ToString());
// Get the second child element from contacts xml tree.
XElement contact2 = contacts.Element("Contact2");
// Create the second TextBlock control.
// Note that the element has to declare two XAML namespaces.
XNamespace xmlns = "http://schemas.microsoft.com/client/2007";
XElement textBlock2 = new XElement(xmlns + "TextBlock",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute("Canvas.Top", 250),
new XAttribute("Width", "600"),
new XAttribute("Text", contact2.ToString())
);
// Add TextBlock control to the page
LayoutRoot.Children.Add(XamlReader.Load(textBlock1.ToString()) as UIElement);
// Add TextBlock control to the page
LayoutRoot.Children.Add(XamlReader.Load(textBlock2.ToString()) as UIElement);