System.Xml.XmlReader.ReadContentAs C# (CSharp) Method

ReadContentAs() public method

public ReadContentAs ( Type returnType, IXmlNamespaceResolver namespaceResolver ) : object
returnType System.Type
namespaceResolver IXmlNamespaceResolver
return object
        public virtual object ReadContentAs(Type returnType, IXmlNamespaceResolver namespaceResolver)
        {
            if (!CanReadContentAs())
            {
                throw CreateReadContentAsException(nameof(ReadContentAs));
            }

            string strContentValue = InternalReadContentAsString();
            if (returnType == typeof(string))
            {
                return strContentValue;
            }
            else
            {
                try
                {
                    return XmlUntypedStringConverter.Instance.FromString(strContentValue, returnType, (namespaceResolver == null ? this as IXmlNamespaceResolver : namespaceResolver));
                }
                catch (FormatException e)
                {
                    throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo);
                }
                catch (InvalidCastException e)
                {
                    throw new XmlException(SR.Xml_ReadContentAsFormatException, returnType.ToString(), e, this as IXmlLineInfo);
                }
            }
        }

Usage Example

Example #1
0
        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.MoveToContent();
            Url         = reader.GetAttribute("Url");
            ServiceName = reader.GetAttribute("ServiceName");

            Boolean isEmptyElement = reader.IsEmptyElement; // (1)

            reader.ReadStartElement();

            if (!isEmptyElement) // (1)
            {
                HostedServiceProperties = reader.ReadContentAs(typeof(AzureHostedServiceProperties), null) as AzureHostedServiceProperties;

                reader.ReadEndElement();
            }

            isEmptyElement = reader.IsEmptyElement; // (1)

            reader.ReadStartElement();

            if (!isEmptyElement) // (1)
            {
                Deployments = reader.ReadContentAs(typeof(List <AzureDeployment>), null) as List <AzureDeployment>;

                reader.ReadEndElement();
            }
        }
All Usage Examples Of System.Xml.XmlReader::ReadContentAs