示例#1
0
        private async Task <XmlRpcResponse> ReadResponseAsync(HttpResponseMessage httpResponse)
        {
            var responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            var responseModelReader = new ResponseModelReader(new ValueReaders(XmlEncoding));

            return(await responseModelReader.ReadAsync(new MemoryStream(XmlEncoding.GetBytes(responseContent)), false).ConfigureAwait(false));
        }
        private void ReadPositionalValue(FlatFileSchemaElement element, Stream stream, BinaryWriter outputWriter)
        {
            var readBytes      = new byte[element.PosLength];
            var readBytesCount = stream.Read(readBytes, 0, element.PosLength);

            if (readBytesCount != element.PosLength)
            {
                throw new Exception($"Unexpected end-of-file detected. Expected value for '{element.Name}'.");
            }

            var valueString = Encoding.GetString(readBytes);

            // If the value is justified, remove the padding characters:
            if (element.Justification == FlatFileElementJustification.Left)
            {
                valueString = valueString.TrimEnd(DefaultPadChar);
            }
            else if (element.Justification == FlatFileElementJustification.Right)
            {
                valueString = valueString.TrimStart(DefaultPadChar);
            }

            // Construct the attribute or element:
            outputWriter.Write(element.StartElement);
            if (element.ElementType == SchemaElementType.Attribute)
            {
                // Write the value:
                // Escape the XML characters:
                var escapedValue = SecurityElement.Escape(valueString);
                if (escapedValue != null)
                {
                    outputWriter.Write(XmlEncoding.GetBytes(escapedValue));
                }

                // Close the attribute (write '"'):
                outputWriter.Write(element.ElementCloser);
            }
            else
            {
                // Close the start element (write '>'):
                outputWriter.Write(element.ElementCloser);

                // Write the value:
                // Escape the XML characters:
                var escapedValue = SecurityElement.Escape(valueString);
                if (escapedValue != null)
                {
                    outputWriter.Write(XmlEncoding.GetBytes(escapedValue));
                }

                // Write the closing xml element to the output stream:
                outputWriter.Write(element.EndElement);
            }
        }
示例#3
0
 /// <summary>
 /// Deserializza una stringa XML nell'oggetto del tipo richiesto. Un valore restituito indica se la deserializzazione è riuscita o meno.
 /// </summary>
 /// <param name="xmlString">Stringa ottenuta dalla serializzazione XML dell'oggetto.</param>
 /// <returns></returns>
 public static bool TryXmlStringToObject <T>(string xmlString, out T obj, params Type[] extraTypes)
     where T : class
 {
     try
     {
         XmlEncoding encoding = XmlEncoding.UTF8;
         obj = XmlStringToObject <T>(xmlString, out encoding, extraTypes);
         return(true);
     }
     catch
     {
         obj = default(T);
         return(false);
     }
 }
示例#4
0
        /// <summary>
        /// Deserializza una stringa XML nell'oggetto del tipo richiesto.
        /// </summary>
        /// <param name="xmlString">Stringa ottenuta dalla serializzazione XML dell'oggetto.</param>
        /// <returns></returns>
        public static T XmlStringToObject <T>(string xmlString, out XmlEncoding encoding, params Type[] extraTypes)
            where T : class
        {
            //xmlString = PreProcess(xmlString);

            T obj;

            //XmlSerializer serializer = new XmlSerializer(typeof(T), extraTypes);
            XmlSerializer serializer = GetXmlSerializer(typeof(T), extraTypes);

            using (MemoryStream ms = new MemoryStream())
            {
                byte[] buffer;

                if (xmlString.Contains("encoding=\"UTF-8\""))
                {
                    encoding = XmlEncoding.UTF8;
                    buffer   = Encoding.UTF8.GetBytes(xmlString);
                }
                else if (xmlString.Contains("encoding=\"UTF-16\""))
                {
                    encoding = XmlEncoding.Unicode;
                    buffer   = Encoding.Unicode.GetBytes(xmlString);
                }
                else
                {
                    encoding = XmlEncoding.ASCII;
                    buffer   = Encoding.UTF8.GetBytes(xmlString);
                }

                ms.Write(buffer, 0, buffer.Length);
                ms.Seek(0, SeekOrigin.Begin);
                obj = (T)serializer.Deserialize(ms);
                ms.Close();
            }

            return(obj);
        }