Exemple #1
0
        /// <summary>
        /// Get the clone from the types derived from this one.
        /// </summary>
        /// <returns>An instance of <see cref="T:UAOOI.SemanticData.UANodeSetValidation.XML.UANode" />.</returns>
        protected override UANode ParentClone()
        {
            UAObjectType _ret = new UAObjectType();

            base.CloneUAType(_ret);
            return(_ret);
        }
        /// <summary>
        /// Imports a node from the set.
        /// </summary>
        private NodeState Import(ISystemContext context, UANode node)
        {
            NodeState importedNode = null;

            NodeClass nodeClass = NodeClass.Unspecified;

            if (node is UAObject)
            {
                nodeClass = NodeClass.Object;
            }
            else if (node is UAVariable)
            {
                nodeClass = NodeClass.Variable;
            }
            else if (node is UAMethod)
            {
                nodeClass = NodeClass.Method;
            }
            else if (node is UAObjectType)
            {
                nodeClass = NodeClass.ObjectType;
            }
            else if (node is UAVariableType)
            {
                nodeClass = NodeClass.VariableType;
            }
            else if (node is UADataType)
            {
                nodeClass = NodeClass.DataType;
            }
            else if (node is UAReferenceType)
            {
                nodeClass = NodeClass.ReferenceType;
            }
            else if (node is UAView)
            {
                nodeClass = NodeClass.View;
            }

            switch (nodeClass)
            {
            case NodeClass.Object:
            {
                UAObject        o     = (UAObject)node;
                BaseObjectState value = new BaseObjectState(null);
                value.EventNotifier = o.EventNotifier;
                importedNode        = value;
                break;
            }

            case NodeClass.Variable:
            {
                UAVariable o = (UAVariable)node;

                NodeId typeDefinitionId = null;

                if (node.References != null)
                {
                    for (int ii = 0; ii < node.References.Length; ii++)
                    {
                        Opc.Ua.NodeId         referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
                        bool                  isInverse       = !node.References[ii].IsForward;
                        Opc.Ua.ExpandedNodeId targetId        = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);

                        if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
                        {
                            typeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            break;
                        }
                    }
                }

                BaseVariableState value = null;

                if (typeDefinitionId == Opc.Ua.VariableTypeIds.PropertyType)
                {
                    value = new PropertyState(null);
                }
                else
                {
                    value = new BaseDataVariableState(null);
                }

                value.DataType                = ImportNodeId(o.DataType, context.NamespaceUris, true);
                value.ValueRank               = o.ValueRank;
                value.ArrayDimensions         = ImportArrayDimensions(o.ArrayDimensions);
                value.AccessLevelEx           = o.AccessLevel;
                value.UserAccessLevel         = (byte)(o.AccessLevel & 0xFF);
                value.MinimumSamplingInterval = o.MinimumSamplingInterval;
                value.Historizing             = o.Historizing;

                if (o.Value != null)
                {
                    XmlDecoder decoder  = CreateDecoder(context, o.Value);
                    TypeInfo   typeInfo = null;
                    value.Value = decoder.ReadVariantContents(out typeInfo);
                    decoder.Close();
                }

                importedNode = value;
                break;
            }

            case NodeClass.Method:
            {
                UAMethod    o     = (UAMethod)node;
                MethodState value = new MethodState(null);
                value.Executable       = o.Executable;
                value.UserExecutable   = o.Executable;
                value.TypeDefinitionId = ImportNodeId(o.MethodDeclarationId, context.NamespaceUris, true);
                importedNode           = value;
                break;
            }

            case NodeClass.View:
            {
                UAView    o     = (UAView)node;
                ViewState value = new ViewState();
                value.ContainsNoLoops = o.ContainsNoLoops;
                importedNode          = value;
                break;
            }

            case NodeClass.ObjectType:
            {
                UAObjectType        o     = (UAObjectType)node;
                BaseObjectTypeState value = new BaseObjectTypeState();
                value.IsAbstract = o.IsAbstract;
                importedNode     = value;
                break;
            }

            case NodeClass.VariableType:
            {
                UAVariableType        o     = (UAVariableType)node;
                BaseVariableTypeState value = new BaseDataVariableTypeState();
                value.IsAbstract      = o.IsAbstract;
                value.DataType        = ImportNodeId(o.DataType, context.NamespaceUris, true);
                value.ValueRank       = o.ValueRank;
                value.ArrayDimensions = ImportArrayDimensions(o.ArrayDimensions);

                if (o.Value != null)
                {
                    XmlDecoder decoder  = CreateDecoder(context, o.Value);
                    TypeInfo   typeInfo = null;
                    value.Value = decoder.ReadVariantContents(out typeInfo);
                    decoder.Close();
                }

                importedNode = value;
                break;
            }

            case NodeClass.DataType:
            {
                UADataType    o     = (UADataType)node;
                DataTypeState value = new DataTypeState();
                value.IsAbstract = o.IsAbstract;
                Opc.Ua.DataTypeDefinition dataTypeDefinition = Import(o, o.Definition, context.NamespaceUris);
                value.DataTypeDefinition = new ExtensionObject(dataTypeDefinition);
                value.Purpose            = o.Purpose;
                value.DataTypeModifier   = DataTypeModifier.None;

                if (o.Definition != null)
                {
                    if (o.Definition.IsOptionSet)
                    {
                        value.DataTypeModifier = DataTypeModifier.OptionSet;
                    }
                    else if (o.Definition.IsUnion)
                    {
                        value.DataTypeModifier = DataTypeModifier.Union;
                    }
                }

                importedNode = value;
                break;
            }

            case NodeClass.ReferenceType:
            {
                UAReferenceType    o     = (UAReferenceType)node;
                ReferenceTypeState value = new ReferenceTypeState();
                value.IsAbstract  = o.IsAbstract;
                value.InverseName = Import(o.InverseName);
                value.Symmetric   = o.Symmetric;
                importedNode      = value;
                break;
            }
            }

            importedNode.NodeId      = ImportNodeId(node.NodeId, context.NamespaceUris, false);
            importedNode.BrowseName  = ImportQualifiedName(node.BrowseName, context.NamespaceUris);
            importedNode.DisplayName = Import(node.DisplayName);

            if (importedNode.DisplayName == null)
            {
                importedNode.DisplayName = new Ua.LocalizedText(importedNode.BrowseName.Name);
            }

            importedNode.Description   = Import(node.Description);
            importedNode.Categories    = (node.Category != null && node.Category.Length > 0) ? node.Category : null;
            importedNode.ReleaseStatus = node.ReleaseStatus;
            importedNode.WriteMask     = (AttributeWriteMask)node.WriteMask;
            importedNode.UserWriteMask = (AttributeWriteMask)node.UserWriteMask;
            importedNode.Extensions    = node.Extensions;

            if (!String.IsNullOrEmpty(node.SymbolicName))
            {
                importedNode.SymbolicName = node.SymbolicName;
            }

            if (node.References != null)
            {
                BaseInstanceState instance = importedNode as BaseInstanceState;
                BaseTypeState     type     = importedNode as BaseTypeState;

                for (int ii = 0; ii < node.References.Length; ii++)
                {
                    Opc.Ua.NodeId         referenceTypeId = ImportNodeId(node.References[ii].ReferenceType, context.NamespaceUris, true);
                    bool                  isInverse       = !node.References[ii].IsForward;
                    Opc.Ua.ExpandedNodeId targetId        = ImportExpandedNodeId(node.References[ii].Value, context.NamespaceUris, context.ServerUris);

                    if (instance != null)
                    {
                        if (referenceTypeId == ReferenceTypeIds.HasModellingRule && !isInverse)
                        {
                            instance.ModellingRuleId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }

                        if (referenceTypeId == ReferenceTypeIds.HasTypeDefinition && !isInverse)
                        {
                            instance.TypeDefinitionId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }
                    }

                    if (type != null)
                    {
                        if (referenceTypeId == ReferenceTypeIds.HasSubtype && isInverse)
                        {
                            type.SuperTypeId = Opc.Ua.ExpandedNodeId.ToNodeId(targetId, context.NamespaceUris);
                            continue;
                        }
                    }

                    importedNode.AddReference(referenceTypeId, isInverse, targetId);
                }
            }

            return(importedNode);
        }
        /// <summary>
        /// Adds a node to the set.
        /// </summary>
        public void Export(ISystemContext context, NodeState node, bool outputRedundantNames = true)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (Opc.Ua.NodeId.IsNull(node.NodeId))
            {
                throw new ArgumentException("A non-null NodeId must be specified.");
            }

            UANode exportedNode = null;

            switch (node.NodeClass)
            {
            case NodeClass.Object:
            {
                BaseObjectState o     = (BaseObjectState)node;
                UAObject        value = new UAObject();
                value.EventNotifier = o.EventNotifier;

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                exportedNode = value;
                break;
            }

            case NodeClass.Variable:
            {
                BaseVariableState o     = (BaseVariableState)node;
                UAVariable        value = new UAVariable();
                value.DataType                = ExportAlias(o.DataType, context.NamespaceUris);
                value.ValueRank               = o.ValueRank;
                value.ArrayDimensions         = Export(o.ArrayDimensions);
                value.AccessLevel             = o.AccessLevelEx;
                value.MinimumSamplingInterval = o.MinimumSamplingInterval;
                value.Historizing             = o.Historizing;

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                if (o.Value != null)
                {
                    XmlEncoder encoder = CreateEncoder(context);

                    Variant variant = new Variant(o.Value);
                    encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                    XmlDocument document = new XmlDocument();
                    document.InnerXml = encoder.Close();
                    value.Value       = document.DocumentElement;
                }

                exportedNode = value;
                break;
            }

            case NodeClass.Method:
            {
                MethodState o     = (MethodState)node;
                UAMethod    value = new UAMethod();
                value.Executable = o.Executable;

                if (o.TypeDefinitionId != null && !o.TypeDefinitionId.IsNullNodeId && o.TypeDefinitionId != o.NodeId)
                {
                    value.MethodDeclarationId = Export(o.TypeDefinitionId, context.NamespaceUris);
                }

                if (o.Parent != null)
                {
                    value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                }

                exportedNode = value;
                break;
            }

            case NodeClass.View:
            {
                ViewState o     = (ViewState)node;
                UAView    value = new UAView();
                value.ContainsNoLoops = o.ContainsNoLoops;
                exportedNode          = value;
                break;
            }

            case NodeClass.ObjectType:
            {
                BaseObjectTypeState o     = (BaseObjectTypeState)node;
                UAObjectType        value = new UAObjectType();
                value.IsAbstract = o.IsAbstract;
                exportedNode     = value;
                break;
            }

            case NodeClass.VariableType:
            {
                BaseVariableTypeState o     = (BaseVariableTypeState)node;
                UAVariableType        value = new UAVariableType();
                value.IsAbstract      = o.IsAbstract;
                value.DataType        = ExportAlias(o.DataType, context.NamespaceUris);
                value.ValueRank       = o.ValueRank;
                value.ArrayDimensions = Export(o.ArrayDimensions);

                if (o.Value != null)
                {
                    XmlEncoder encoder = CreateEncoder(context);

                    Variant variant = new Variant(o.Value);
                    encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                    XmlDocument document = new XmlDocument();
                    document.InnerXml = encoder.Close();
                    value.Value       = document.DocumentElement;
                }

                exportedNode = value;
                break;
            }

            case NodeClass.DataType:
            {
                DataTypeState o     = (DataTypeState)node;
                UADataType    value = new UADataType();
                value.IsAbstract = o.IsAbstract;
                value.Definition = Export(o, o.DataTypeDefinition, context.NamespaceUris, outputRedundantNames);
                value.Purpose    = o.Purpose;
                exportedNode     = value;
                break;
            }

            case NodeClass.ReferenceType:
            {
                ReferenceTypeState o     = (ReferenceTypeState)node;
                UAReferenceType    value = new UAReferenceType();
                value.IsAbstract = o.IsAbstract;

                if (!Opc.Ua.LocalizedText.IsNullOrEmpty(o.InverseName))
                {
                    value.InverseName = Export(new Opc.Ua.LocalizedText[] { o.InverseName });
                }

                value.Symmetric = o.Symmetric;
                exportedNode    = value;
                break;
            }
            }

            exportedNode.NodeId     = Export(node.NodeId, context.NamespaceUris);
            exportedNode.BrowseName = Export(node.BrowseName, context.NamespaceUris);

            if (outputRedundantNames || node.DisplayName.Text != node.BrowseName.Name)
            {
                exportedNode.DisplayName = Export(new Opc.Ua.LocalizedText[] { node.DisplayName });
            }
            else
            {
                exportedNode.DisplayName = null;
            }

            if (node.Description != null && !String.IsNullOrEmpty(node.Description.Text))
            {
                exportedNode.Description = Export(new Opc.Ua.LocalizedText[] { node.Description });
            }
            else
            {
                exportedNode.Description = new LocalizedText[0];
            }

            exportedNode.Category      = (node.Categories != null && node.Categories.Count > 0) ? new List <string>(node.Categories).ToArray() : null;
            exportedNode.ReleaseStatus = node.ReleaseStatus;
            exportedNode.WriteMask     = (uint)node.WriteMask;
            exportedNode.UserWriteMask = (uint)node.UserWriteMask;
            exportedNode.Extensions    = node.Extensions;

            if (!String.IsNullOrEmpty(node.SymbolicName) && node.SymbolicName != node.BrowseName.Name)
            {
                exportedNode.SymbolicName = node.SymbolicName;
            }

            // export references.
            INodeBrowser     browser            = node.CreateBrowser(context, null, null, true, BrowseDirection.Both, null, null, true);
            List <Reference> exportedReferences = new List <Reference>();
            IReference       reference          = browser.Next();

            while (reference != null)
            {
                if (node.NodeClass == NodeClass.Method)
                {
                    if (!reference.IsInverse && reference.ReferenceTypeId == ReferenceTypeIds.HasTypeDefinition)
                    {
                        reference = browser.Next();
                        continue;
                    }
                }

                Reference exportedReference = new Reference();

                exportedReference.ReferenceType = ExportAlias(reference.ReferenceTypeId, context.NamespaceUris);
                exportedReference.IsForward     = !reference.IsInverse;
                exportedReference.Value         = Export(reference.TargetId, context.NamespaceUris, context.ServerUris);
                exportedReferences.Add(exportedReference);

                reference = browser.Next();
            }

            exportedNode.References = exportedReferences.ToArray();

            // add node to list.
            UANode[] nodes = null;

            int count = 1;

            if (this.Items == null)
            {
                nodes = new UANode[count];
            }
            else
            {
                count += this.Items.Length;
                nodes  = new UANode[count];
                Array.Copy(this.Items, nodes, this.Items.Length);
            }

            nodes[count - 1] = exportedNode;

            this.Items = nodes;

            // recusively process children.
            List <BaseInstanceState> children = new List <BaseInstanceState>();

            node.GetChildren(context, children);

            for (int ii = 0; ii < children.Count; ii++)
            {
                Export(context, children[ii], outputRedundantNames);
            }
        }
Exemple #4
0
 private static void Update(IObjectTypeFactory nodeDesign, UAObjectType nodeSet)
 {
 }
        // OPC UA defines variables, views and objects, as well as associated variabletypes, datatypes, referencetypes and objecttypes
        // In addition, OPC UA defines methods and properties
        public static void Generate(UANodeSet nodeSet)
        {
            // clear previously generated DTDL
            _map.Clear();
            _interfaceList.Clear();
            _contentsList.Clear();
            _nodeList.Clear();
            _nodesetNamespaceURI = nodeSet.NamespaceUris[0];

            CreateSchemaMap();

            // create DTDL interfaces and their contents
            foreach (UANode uaNode in nodeSet.Items)
            {
                UAVariable variable = uaNode as UAVariable;
                if (variable != null)
                {
                    if (uaNode.BrowseName.ToString() == "InputArguments")
                    {
                        continue;
                    }

                    // check if this node is part of the model
                    bool isPartOfModel = false;
                    foreach (Reference reference in variable.References)
                    {
                        if (reference.ReferenceType == "HasModellingRule")
                        {
                            isPartOfModel = true;
                            break;
                        }
                    }
                    if (isPartOfModel)
                    {
                        // ignore this node
                        continue;
                    }

                    DtdlContents dtdlTelemetry = new DtdlContents
                    {
                        Type   = "Telemetry",
                        Name   = Regex.Replace(uaNode.BrowseName.ToString().Trim(), "[^A-Za-z]+", ""),
                        Schema = GetDtdlDataType(variable.DataType)
                    };

                    Tuple <DtdlContents, string> newTuple = new Tuple <DtdlContents, string>(dtdlTelemetry, variable.ParentNodeId);
                    if (!_contentsList.Contains(newTuple))
                    {
                        _contentsList.Add(newTuple);
                    }

                    Tuple <string, string, string> newNodeTuple;
                    if (variable.BrowseName.Length > 0)
                    {
                        newNodeTuple = new Tuple <string, string, string>(variable.BrowseName, GetDtdlDataType(variable.DataType.ToString()), variable.ParentNodeId ?? "");
                    }
                    else
                    {
                        newNodeTuple = new Tuple <string, string, string>(variable.NodeId.ToString(), GetDtdlDataType(variable.DataType.ToString()), variable.ParentNodeId ?? "");
                    }

                    string key = nodeSet.NamespaceUris[0] + "#" + variable.NodeId.ToString().Substring(variable.NodeId.ToString().IndexOf(';') + 1);
                    if (!_nodeList.ContainsKey(key))
                    {
                        _nodeList.Add(key, newNodeTuple);
                    }

                    continue;
                }

                UAMethod method = uaNode as UAMethod;
                if (method != null)
                {
                    // check if this node is part of the model
                    bool isPartOfModel = false;
                    foreach (Reference reference in method.References)
                    {
                        if (reference.ReferenceType == "HasModellingRule")
                        {
                            isPartOfModel = true;
                            break;
                        }
                    }
                    if (isPartOfModel)
                    {
                        // ignore this node
                        continue;
                    }

                    DtdlContents dtdlCommand = new DtdlContents
                    {
                        Type = "Command",
                        Name = Regex.Replace(uaNode.BrowseName.ToString().Trim(), "[^A-Za-z]+", "")
                    };

                    Tuple <DtdlContents, string> newTuple = new Tuple <DtdlContents, string>(dtdlCommand, method.ParentNodeId);
                    if (!_contentsList.Contains(newTuple))
                    {
                        _contentsList.Add(newTuple);
                    }

                    Tuple <string, string, string> newNodeTuple;
                    if (method.BrowseName.Length > 0)
                    {
                        newNodeTuple = new Tuple <string, string, string>(method.BrowseName, "command", method.ParentNodeId ?? "");
                    }
                    else
                    {
                        newNodeTuple = new Tuple <string, string, string>(method.NodeId.ToString(), "command", method.ParentNodeId ?? "");
                    }

                    string key = nodeSet.NamespaceUris[0] + "#" + method.NodeId.ToString().Substring(method.NodeId.ToString().IndexOf(';') + 1);
                    if (!_nodeList.ContainsKey(key))
                    {
                        _nodeList.Add(key, newNodeTuple);
                    }

                    continue;
                }

                UAObject uaObject = uaNode as UAObject;
                if (uaObject != null)
                {
                    // check if this node is part of the model
                    bool isPartOfModel = false;
                    foreach (Reference reference in uaObject.References)
                    {
                        if (reference.ReferenceType == "HasModellingRule")
                        {
                            isPartOfModel = true;
                            break;
                        }
                    }
                    if (isPartOfModel)
                    {
                        // ignore this node
                        continue;
                    }

                    DtdlInterface dtdlInterface = new DtdlInterface
                    {
                        Id          = "dtmi:" + Regex.Replace(uaNode.BrowseName.ToString().Trim(), "[^A-Za-z]+", "") + ";1",
                        Type        = "Interface",
                        DisplayName = Regex.Replace(uaNode.BrowseName.ToString().Trim(), "[^A-Za-z]+", ""),
                        Contents    = new List <DtdlContents>()
                    };

                    Tuple <DtdlInterface, string, string> newTuple = new Tuple <DtdlInterface, string, string>(dtdlInterface, uaObject.NodeId, uaObject.ParentNodeId);
                    if (!_interfaceList.Contains(newTuple))
                    {
                        _interfaceList.Add(newTuple);
                    }

                    Tuple <string, string, string> newNodeTuple;
                    if (uaObject.BrowseName.Length > 0)
                    {
                        newNodeTuple = new Tuple <string, string, string>(uaObject.BrowseName, "object", uaObject.ParentNodeId ?? "");
                    }
                    else
                    {
                        newNodeTuple = new Tuple <string, string, string>(uaObject.NodeId.ToString(), "object", uaObject.ParentNodeId ?? "");
                    }

                    string key = nodeSet.NamespaceUris[0] + "#" + uaObject.NodeId.ToString().Substring(uaObject.NodeId.ToString().IndexOf(';') + 1);
                    if (!_nodeList.ContainsKey(key))
                    {
                        _nodeList.Add(key, newNodeTuple);
                    }

                    continue;
                }

                UAView view = uaNode as UAView;
                if (view != null)
                {
                    // we don't map views since DTDL has no such concept
                    continue;
                }

                UAVariableType variableType = uaNode as UAVariableType;
                if (variableType != null)
                {
                    // we don't map UA variable types, only instances. DTDL only has a limited set of built-in types.
                    continue;
                }

                UADataType dataType = uaNode as UADataType;
                if (dataType != null)
                {
                    // we don't map UA data types, only instances. DTDL only has a limited set of built-in types.
                    continue;
                }

                UAReferenceType referenceType = uaNode as UAReferenceType;
                if (referenceType != null)
                {
                    // we don't map UA reference types, only instances. DTDL only has a limited set of built-in types.
                    continue;
                }

                UAObjectType objectType = uaNode as UAObjectType;
                if (objectType != null)
                {
                    // we don't map UA object (custom) types, only instances. DTDL only has a limited set of built-in types.
                    continue;
                }

                throw new ArgumentException("Unknown UA node detected!");
            }

            AddComponentsToInterfaces();
            AddRelationshipsBetweenInterfaces();

            // generate JSON files
            foreach (Tuple <DtdlInterface, string, string> dtdlInterfaceTuple in _interfaceList)
            {
                string generatedDTDL = JsonConvert.SerializeObject(dtdlInterfaceTuple.Item1, Formatting.Indented);
                string dtdlPath      = Path.Combine(Directory.GetCurrentDirectory(), "JSON", Path.GetFileNameWithoutExtension(dtdlInterfaceTuple.Item1.DisplayName) + ".dtdl.json");
                System.IO.File.WriteAllText(dtdlPath, generatedDTDL);
            }
        }
        /// <summary>
        /// Adds a node to the set.
        /// </summary>
        public void Export(ISystemContext context, NodeState node)
        {
            if (node == null) throw new ArgumentNullException("node");

            if (Opc.Ua.NodeId.IsNull(node.NodeId))
            {
                throw new ArgumentException("A non-null NodeId must be specified.");
            }

            UANode exportedNode = null;

            switch (node.NodeClass)
            {
                case NodeClass.Object:
                {
                    BaseObjectState o = (BaseObjectState)node;
                    UAObject value = new UAObject();
                    value.EventNotifier = o.EventNotifier;

                    if (o.Parent != null)
                    {
                        value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                    }

                    exportedNode = value;
                    break;
                }

                case NodeClass.Variable:
                {
                    BaseVariableState o = (BaseVariableState)node;
                    UAVariable value = new UAVariable();
                    value.DataType = ExportAlias(o.DataType, context.NamespaceUris);
                    value.ValueRank = o.ValueRank;
                    value.ArrayDimensions = Export(o.ArrayDimensions);
                    value.AccessLevel = o.AccessLevel;
                    value.UserAccessLevel = o.UserAccessLevel;
                    value.MinimumSamplingInterval = o.MinimumSamplingInterval;
                    value.Historizing = o.Historizing;

                    if (o.Parent != null)
                    {
                        value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                    }

                    if (o.Value != null)
                    {
                        XmlEncoder encoder = CreateEncoder(context);

                        Variant variant = new Variant(o.Value);
                        encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                        XmlDocument document = new XmlDocument();
                        document.InnerXml = encoder.Close();
                        value.Value = document.DocumentElement;
                    }

                    exportedNode = value;                 
                    break;
                }

                case NodeClass.Method:
                {
                    MethodState o = (MethodState)node;
                    UAMethod value = new UAMethod();
                    value.Executable = o.Executable;
                    value.UserExecutable = o.UserExecutable;

                    if (o.Parent != null)
                    {
                        value.ParentNodeId = ExportAlias(o.Parent.NodeId, context.NamespaceUris);
                    }

                    exportedNode = value;
                    break;
                }

                case NodeClass.View:
                {
                    ViewState o = (ViewState)node;
                    UAView value = new UAView();
                    value.ContainsNoLoops = o.ContainsNoLoops;
                    exportedNode = value;
                    break;
                }

                case NodeClass.ObjectType:
                {
                    BaseObjectTypeState o = (BaseObjectTypeState)node;
                    UAObjectType value = new UAObjectType();
                    value.IsAbstract = o.IsAbstract;
                    exportedNode = value;
                    break;
                }

                case NodeClass.VariableType:
                {
                    BaseVariableTypeState o = (BaseVariableTypeState)node;
                    UAVariableType value = new UAVariableType();
                    value.IsAbstract = o.IsAbstract;
                    value.DataType = ExportAlias(o.DataType, context.NamespaceUris);
                    value.ValueRank = o.ValueRank;
                    value.ArrayDimensions = Export(o.ArrayDimensions);

                    if (o.Value != null)
                    {
                        XmlEncoder encoder = CreateEncoder(context);

                        Variant variant = new Variant(o.Value);
                        encoder.WriteVariantContents(variant.Value, variant.TypeInfo);

                        XmlDocument document = new XmlDocument();
                        document.InnerXml = encoder.Close();
                        value.Value = document.DocumentElement;
                    }

                    exportedNode = value;
                    break;
                }

                case NodeClass.DataType:
                {
                    DataTypeState o = (DataTypeState)node;
                    UADataType value = new UADataType();
                    value.IsAbstract = o.IsAbstract;
                    value.Definition = Export(o.Definition, context.NamespaceUris);
                    exportedNode = value;
                    break;
                }

                case NodeClass.ReferenceType:
                {
                    ReferenceTypeState o = (ReferenceTypeState)node;
                    UAReferenceType value = new UAReferenceType();
                    value.IsAbstract = o.IsAbstract;
                    value.InverseName = Export(new Opc.Ua.LocalizedText[] { o.InverseName });
                    value.Symmetric = o.Symmetric;
                    exportedNode = value;
                    break;
                }
            }

            exportedNode.NodeId = Export(node.NodeId, context.NamespaceUris);
            exportedNode.BrowseName = Export(node.BrowseName, context.NamespaceUris);
            exportedNode.DisplayName = Export(new Opc.Ua.LocalizedText[] { node.DisplayName });
            exportedNode.Description = Export(new Opc.Ua.LocalizedText[] { node.Description });
            exportedNode.WriteMask = (uint)node.WriteMask;
            exportedNode.UserWriteMask = (uint)node.UserWriteMask;

            if (!String.IsNullOrEmpty(node.SymbolicName) && node.SymbolicName != node.BrowseName.Name)
            {
                exportedNode.SymbolicName = node.SymbolicName;
            }

            // export references.
            INodeBrowser browser = node.CreateBrowser(context, null, null, true, BrowseDirection.Both, null, null, true);
            List<Reference> exportedReferences = new List<Reference>();            
            IReference reference = browser.Next();

            while (reference != null)
            {
                Reference exportedReference = new Reference();

                exportedReference.ReferenceType = ExportAlias(reference.ReferenceTypeId, context.NamespaceUris);
                exportedReference.IsForward = !reference.IsInverse;
                exportedReference.Value = Export(reference.TargetId, context.NamespaceUris, context.ServerUris);
                exportedReferences.Add(exportedReference);

                reference = browser.Next();
            }

            exportedNode.References = exportedReferences.ToArray();

            // add node to list.
            UANode[] nodes = null;

            int count = 1;

            if (this.Items == null)
            {
                nodes = new UANode[count];
            }
            else
            {
                count += this.Items.Length;
                nodes = new UANode[count];
                Array.Copy(this.Items, nodes, this.Items.Length);
            }

            nodes[count-1] = exportedNode;

            this.Items = nodes;

            // recusively process children.
            List<BaseInstanceState> children = new List<BaseInstanceState>();
            node.GetChildren(context, children);

            for (int ii = 0; ii < children.Count; ii++)
            {
                Export(context, children[ii]);
            }
        }