Opc.Ua.XmlDecoder.ReadVariantContents C# (CSharp) Method

ReadVariantContents() private method

private ReadVariantContents ( TypeInfo &typeInfo ) : object
typeInfo TypeInfo
return object
        public object ReadVariantContents(out TypeInfo typeInfo)
        {
            typeInfo = TypeInfo.Unknown;

            // skip whitespace.
            while (m_reader.NodeType != XmlNodeType.Element)
            {
                m_reader.Read();
            }

            try
            {
                m_namespaces.Push(Namespaces.OpcUaXsd);

                string typeName = m_reader.LocalName;

                // process array types.
                if (typeName.StartsWith("ListOf", StringComparison.Ordinal))
                {
                    switch (typeName.Substring("ListOf".Length))
                    {
                        case "Boolean":
                            {
                                typeInfo = TypeInfo.Arrays.Boolean;
                                BooleanCollection collection = ReadBooleanArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "SByte":
                            {
                                typeInfo = TypeInfo.Arrays.SByte;
                                SByteCollection collection = ReadSByteArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Byte":
                            {
                                typeInfo = TypeInfo.Arrays.Byte;
                                ByteCollection collection = ReadByteArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Int16":
                            {
                                typeInfo = TypeInfo.Arrays.Int16;
                                Int16Collection collection = ReadInt16Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "UInt16":
                            {
                                typeInfo = TypeInfo.Arrays.UInt16;
                                UInt16Collection collection = ReadUInt16Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Int32":
                            {
                                typeInfo = TypeInfo.Arrays.Int32;
                                Int32Collection collection = ReadInt32Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "UInt32":
                            {
                                typeInfo = TypeInfo.Arrays.UInt32;
                                UInt32Collection collection = ReadUInt32Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Int64":
                            {
                                typeInfo = TypeInfo.Arrays.Int64;
                                Int64Collection collection = ReadInt64Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "UInt64":
                            {
                                typeInfo = TypeInfo.Arrays.UInt64; 
                                UInt64Collection collection = ReadUInt64Array(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Float":
                            {
                                typeInfo = TypeInfo.Arrays.Float;
                                FloatCollection collection = ReadFloatArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Double":
                            {
                                typeInfo = TypeInfo.Arrays.Double;
                                DoubleCollection collection = ReadDoubleArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "String":
                            {
                                typeInfo = TypeInfo.Arrays.String;
                                StringCollection collection = ReadStringArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "DateTime":
                            {
                                typeInfo = TypeInfo.Arrays.DateTime;
                                DateTimeCollection collection = ReadDateTimeArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Guid":
                            {
                                typeInfo = TypeInfo.Arrays.Guid;
                                UuidCollection collection = ReadGuidArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "ByteString":
                            {
                                typeInfo = TypeInfo.Arrays.ByteString;
                                ByteStringCollection collection = ReadByteStringArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "XmlElement":
                            {
                                typeInfo = TypeInfo.Arrays.XmlElement;
                                XmlElementCollection collection = ReadXmlElementArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "NodeId":
                            {
                                typeInfo = TypeInfo.Arrays.NodeId;
                                NodeIdCollection collection = ReadNodeIdArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "ExpandedNodeId":
                            {
                                typeInfo = TypeInfo.Arrays.ExpandedNodeId;
                                ExpandedNodeIdCollection collection = ReadExpandedNodeIdArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "StatusCode":
                            {
                                typeInfo = TypeInfo.Arrays.StatusCode;
                                StatusCodeCollection collection = ReadStatusCodeArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "DiagnosticInfo":
                            {
                                typeInfo = TypeInfo.Arrays.DiagnosticInfo;
                                DiagnosticInfoCollection collection = ReadDiagnosticInfoArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "QualifiedName":
                            {
                                typeInfo = TypeInfo.Arrays.QualifiedName;
                                QualifiedNameCollection collection = ReadQualifiedNameArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "LocalizedText":
                            {
                                typeInfo = TypeInfo.Arrays.LocalizedText;
                                LocalizedTextCollection collection = ReadLocalizedTextArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "ExtensionObject":
                            {
                                typeInfo = TypeInfo.Arrays.ExtensionObject;
                                ExtensionObjectCollection collection = ReadExtensionObjectArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "DataValue":
                            {
                                typeInfo = TypeInfo.Arrays.DataValue;
                                DataValueCollection collection = ReadDataValueArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                        case "Variant":
                            {
                                typeInfo = TypeInfo.Arrays.Variant;
                                VariantCollection collection = ReadVariantArray(typeName);
                                if (collection != null) return collection.ToArray();
                                return null;
                            }
                    }
                }

                // process scalar types.
                else
                {
                    switch (typeName)
                    {
                        case "Null":            
                        {
                            if (BeginField(typeName, true))
                            {
                                EndField(typeName);
                            }

                            return null;
                        }

                        case "Boolean":         { typeInfo = TypeInfo.Scalars.Boolean; return ReadBoolean(typeName); }
                        case "SByte":           { typeInfo = TypeInfo.Scalars.SByte; return ReadSByte(typeName); }
                        case "Byte":            { typeInfo = TypeInfo.Scalars.Byte; return ReadByte(typeName); }
                        case "Int16":           { typeInfo = TypeInfo.Scalars.Int16; return ReadInt16(typeName); }
                        case "UInt16":          { typeInfo = TypeInfo.Scalars.UInt16; return ReadUInt16(typeName); }
                        case "Int32":           { typeInfo = TypeInfo.Scalars.Int32; return ReadInt32(typeName); }
                        case "UInt32":          { typeInfo = TypeInfo.Scalars.UInt32; return ReadUInt32(typeName); }
                        case "Int64":           { typeInfo = TypeInfo.Scalars.Int64; return ReadInt64(typeName); }
                        case "UInt64":          { typeInfo = TypeInfo.Scalars.UInt64; return ReadUInt64(typeName); }
                        case "Float":           { typeInfo = TypeInfo.Scalars.Float; return ReadFloat(typeName); }
                        case "Double":          { typeInfo = TypeInfo.Scalars.Double; return ReadDouble(typeName); }
                        case "String":          { typeInfo = TypeInfo.Scalars.String; return ReadString(typeName); }
                        case "DateTime":        { typeInfo = TypeInfo.Scalars.DateTime; return ReadDateTime(typeName); }
                        case "Guid":            { typeInfo = TypeInfo.Scalars.Guid; return ReadGuid(typeName); }
                        case "ByteString":      { typeInfo = TypeInfo.Scalars.ByteString; return ReadByteString(typeName); }
                        case "XmlElement":      { typeInfo = TypeInfo.Scalars.XmlElement; return ReadXmlElement(typeName); }
                        case "NodeId":          { typeInfo = TypeInfo.Scalars.NodeId; return ReadNodeId(typeName); }
                        case "ExpandedNodeId":  { typeInfo = TypeInfo.Scalars.ExpandedNodeId; return ReadExpandedNodeId(typeName); }
                        case "StatusCode":      { typeInfo = TypeInfo.Scalars.StatusCode; return ReadStatusCode(typeName); }
                        case "DiagnosticInfo":  { typeInfo = TypeInfo.Scalars.DiagnosticInfo; return ReadDiagnosticInfo(typeName); }                        
                        case "QualifiedName":   { typeInfo = TypeInfo.Scalars.QualifiedName; return ReadQualifiedName(typeName); }
                        case "LocalizedText":   { typeInfo = TypeInfo.Scalars.LocalizedText; return ReadLocalizedText(typeName); }
                        case "ExtensionObject": { typeInfo = TypeInfo.Scalars.ExtensionObject; return ReadExtensionObject(typeName); }
                        case "DataValue":       { typeInfo = TypeInfo.Scalars.DataValue; return ReadDataValue(typeName); }

                        case "Matrix": 
                        {
                            Matrix matrix = ReadMatrix(typeName);
                            typeInfo = matrix.TypeInfo;
                            return matrix;     
                        }
                    }
                }
                            
                throw new ServiceResultException(
                    StatusCodes.BadDecodingError,
                    Utils.Format("Element '{1}:{0}' is not allowed in an Variant.", m_reader.LocalName, m_reader.NamespaceURI)); 
            }
            finally
            {
                m_namespaces.Pop();
            }
        }
        

Usage Example

コード例 #1
0
        /// <summary>
        /// Converts the XML back to a value.
        /// </summary>
        private Variant GetValue()
        {
            if (!m_textChanged)
            {
                return m_value;
            }

            XmlDocument document = new XmlDocument();
            document.InnerXml = ValueTB.Text;
            
            // find the first element.
            XmlElement element = null;
            
            for (XmlNode node = document.DocumentElement.FirstChild; node != null; node = node.NextSibling)
            {
                element = node as XmlElement;

                if (element != null)
                {
                    break;
                }
            }

            XmlDecoder decoder = new XmlDecoder(element, m_session.MessageContext);

            decoder.PushNamespace(Namespaces.OpcUaXsd);
            TypeInfo typeInfo = null;
            object value = decoder.ReadVariantContents(out typeInfo);

            return new Variant(value, typeInfo);
        }