XML Styles

Although the yFiles FLEX client API and the yFiles.NET library use the same style concept for the visual representation of graph items, it is possible that there is no corresponding style available on the server for a style implementation that is used on the client. It is also possible that a style exists on the client and on the server, but the client uses a style property value that does not exist on the server. In both cases, the yFiles FLEX .NET server library still allows graph roundtripping and even style manipulation.

In general, an XmlStyle<TModelItem, TStyle> should be used whenever there is no IDeserializer available for deserializing a given style. This can be achieved automatically by using a XmlStyleInputHandler<TModelItem, TStyle, TXmlStyle> for graph deserialization. ConfigureHandler in GraphRoundtripSupport already registers XmlStyleInputHandlers for all graph item types (nodes, edges, ports, and labels). The XmlStyleInputHandler will use a special IDeserializer implementation that creates an XMLStyle, if no other style deserializer is found.

An XmlStyle serves as a container for the raw XML data that represents a serialization of an unknown style. The XML data can be retrieved and manipulated using the XmlNode property. When an XmlStyle is serialized, the XML snippet contained in its XmlNode property is written.

All XmlStyles have a FallbackStyle property. The style that is set as the fallback style of an XmlStyle is used for drawing the graph item that is associated with the XmlStyle.

In addition to the XmlStyle class, the class XmlArrow is available in the yFiles FLEX .NET library. XmlArrow can be used for arrow roundtripping if an arrow can not be properly deserialized by an IDeserializer. Since the yFiles FLEX client comes with arrow types that are not supported by yFiles.NET, a deserializer for XmlArrows is registered in ConfigureHandler.

The following example demonstrates how to manipulate style data that is only available as an XmlNode in an XmlStyle instance. The code example is taken from the custom style demo application that is included as source code in the yFiles FLEX distribution.

Example 8.2. Manipulating XML style data

XmlNodeStyle xmlStyle = node.Style as XmlNodeStyle;
if (null != xmlStyle) {
  // The XmlNode contains the style data for the custom node style
  XmlNode xmlNode = xmlStyle.XmlNode;
  
  if ("CustomStyle".Equals(xmlNode.LocalName)) {
    // Add the yFiles namespace
    XmlNamespaceManager nsmgr = 
      new XmlNamespaceManager(xmlNode.OwnerDocument.NameTable);
    nsmgr.AddNamespace(GraphMLConstants.YFILES_NS_PREFIX, 
                       GraphMLConstants.YFILES_NS);
    
    // Find the "Percentage" element
    XmlNode percentageNode = 
      xmlNode.SelectSingleNode("descendant::y:Percentage", nsmgr);
    
    // set the "value" of the Percentage element to val
    XmlAttribute a = percentageNode.Attributes["value"];
    if (null != a) {
      string strVal = XmlConvert.ToString(val);
      a.Value = strVal;
    }
  }
}