GoDigram for .NET Framework and .NET Core
Northwoods.Go.Xml Namespace / GoXmlWriter Class
Members Example
In This Topic
    GoXmlWriter Class
    In This Topic
    Write customizable XML, using class-specific IGoXmlTransformers, creating an XmlDocument or just writing to an XmlWriter.
    Syntax
    public class GoXmlWriter : GoXmlReaderWriterBase 
    Remarks

    To produce XML, this class opens a XmlWriter, generates XML instructions, starts a root element, and then iterates twice over the set of Objects. The first pass (implemented by GenerateDefinitions) gives each object a chance to detect and remember shared objects and set up other definitions in preliminary XML elements that are part of the root. The second pass (implemented by GenerateObjects) actually produces elements for the Objects.

    You must provide type-specific customizations by supplying instances of IGoXmlTransformer. Each transformer is associated with a Type. As this writer processes each object that it is rendering, it searches for the appropriate transformer to invoke, starting with that type and trying its base types.

    By default there are no transformers registered for this writer, so this writer is unable to actually do anything with the specified Objects. You will need to call GoXmlReaderWriterBase.AddTransformer for each class of object for which you want to produce XML. These calls to GoXmlReaderWriterBase.AddTransformer are sometimes done in an override of GoXmlReaderWriterBase.RegisterTransformers, but you probably do not need to define a class inheriting from GoXmlWriter.

    For the GenerateDefinitions pass, DefineObject is called for each object in Objects, which in turn calls IGoXmlTransformer.GenerateDefinitions on the registered transformer for the object's Type.

    For the GenerateObjects pass, GenerateObject is called for each object in Objects, which in turn calls GenerateElement, GenerateAttributes, GenerateBody, and IGoXmlTransformer.GenerateElementFinish on the registered transformer for the object's Type.

    The various Invoke... methods do the actual lookup for a transformer and invoke the respective method on the first transformer found.

    Example
    This writer could be used as follows: See the description of GoXmlTransformer for example transformer definitions.
    public void SaveSimpleXml(String path) {
      GoXmlWriter xw = new GoXmlWriter();
      xw.RootElementName = "graph";
      xw.NodesGeneratedFirst = true;
      // tell the writer how to handle two kinds of classes
      xw.AddTransformer(new SimpleXmlTransformBasicNode());
      xw.AddTransformer(new SimpleXmlTransformLink());
      // specify the objects to be generated
      xw.Objects = myView.Document;
      
      using (StreamWriter sw = new StreamWriter(path)) {
        xw.Generate(sw);
      }
    }
    See Also