Search
Tutorial 2: Loading components from XML

In the previous tutorial (Setting a composite node) we examined how to create composite node and several components. In this example we will create a composite node with the same component structure but instead of defining the components in code we will load them from XML. Here is the XML equivalent to the component hierarchy created in the previous tutorial.

XML  Copy Code

<StackPanel Orientation="Horizontal" Spacing="1">
  <Text Text="Enter name:" />
  <Edit Width="25" AcceptsReturn="False" />
  <Button Content="Enter" />
</StackPanel>

Assign the above XML to a string variable named xmlContent. Then the code that creates the composite node and loads the above XML looks like this:

C#  Copy Code

CompositeNode node = new CompositeNode(diagram1);
node.Components.Add(XmlLoader.Load(xmlContent));
node.Bounds = new RectangleF(10, 10, 60, 10);
diagram1.Nodes.Add(node);

Visual Basic  Copy Code

Dim node As New CompositeNode(diagram1)
node.Components.Add(XmlLoader.Load(xmlContent))
node.Bounds = New RectangleF(10, 10, 60, 10)
diagram1.Nodes.Add(node)

Running the tutorial will yield the same result yet the declaration of the component hierarchy is much more compact.