Example #1
0
        /// <summary>
        /// Deserializes object from isolated storage file.
        /// </summary>
        /// <typeparam name="T">Type of object.</typeparam>
        /// <param name="fileName">Path to isolated storage file.</param>
        /// <returns></returns>
        public static T Deserialize <T>(string subDirectory, string fileName)
        {
            try
            {
                // Open isolated storage.
                using (var storageManager = new IsolatedStorageManager())
                {
                    fileName = Prepare(storageManager, subDirectory, fileName);

                    if (storageManager.FileExists(fileName))
                    {
                        // Open file from storage.
                        using (var stream = storageManager.OpenFile(fileName, IO.OpenFileMode.Open))
                        {
                            XFile file = XFile.LoadBinary(stream, fileName);

                            var serializer = new XSerializer(file);

                            return(serializer.Deserialize <T>("Data"));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }

            return(default(T));
        }
Example #2
0
 public static T ToObject <T>(this string text)
 {
     if (text.IsNullOrEmpty())
     {
         return(default(T));
     }
     return(XSerializer.Deserialize <T>(text));
 }
 private XResult <T> Deserialize <T>(String xml) where T : FOCommonResponse
 {
     try
     {
         var result = _serializer.Deserialize <T>(xml);
         return(new XResult <T>(result));
     }
     catch (Exception ex)
     {
         return(new XResult <T>(null, ErrorCode.DESERIALIZE_FAILED, ex));
     }
 }
Example #4
0
        }         // enum LegalStatus

        public TargetResults(string targetData)
        {
            OutStr  = targetData;
            Targets = new List <CompanyInfo>();

            try
            {
                foreach (var business in XElement.Parse(targetData).Element("REQUEST").Elements("DT11"))
                {
                    var bi = XSerializer.Deserialize <CompanyInfo>(business);
                    Targets.Add(bi);
                }                 // for each business

                if (Targets.Any())
                {
                    foreach (var t in Targets)
                    {
                        t.BusName = string.IsNullOrEmpty(t.BusName)
                                                        ? string.Empty
                                                        : System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t.BusName.ToLower());
                        t.AddrLine1 = string.IsNullOrEmpty(t.AddrLine1)
                                                        ? string.Empty
                                                        : System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t.AddrLine1.ToLower());
                        t.AddrLine2 = string.IsNullOrEmpty(t.AddrLine2)
                                                        ? string.Empty
                                                        : System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t.AddrLine2.ToLower());
                        t.AddrLine3 = string.IsNullOrEmpty(t.AddrLine3)
                                                        ? string.Empty
                                                        : System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t.AddrLine3.ToLower());
                        t.AddrLine4 = string.IsNullOrEmpty(t.AddrLine4)
                                                        ? string.Empty
                                                        : System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(t.AddrLine4.ToLower());
                    }                     // for each

                    if (Targets.Count > 1)
                    {
                        Targets.Add(new CompanyInfo {
                            BusName = "Company not found", BusRefNum = "skip"
                        });
                    }
                }                 // if
            }
            catch
            {
            } // try
        }     // constructor
Example #5
0
        /// <summary>
        /// Deserializes data string with Xml Serializer
        /// </summary>
        /// <typeparam name="ObjetType">Type of returned object</typeparam>
        /// <param name="data">serialized data</param>
        /// <returns>returns deserialized object of specified type</returns>
        public static T Deserialize <T>(string data, string rootAttribute = null)//this string data
        {
            XmlSerializer XSerializer;

            if (System.String.IsNullOrEmpty(rootAttribute))
            {
                XSerializer = new XmlSerializer(typeof(T));
            }
            else
            {
                XSerializer = new XmlSerializer(typeof(T), rootAttribute);
            }

            StringReader SReader = new StringReader(data);
            T            output  = (T)XSerializer.Deserialize(SReader);

            SReader.Close();

            return(output);
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The created object.</returns>
        public virtual object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XAttribute lAttribute         = this.mTargetElement.Attribute(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE);
            string     lExternalReference = lAttribute.Value;
            string     lFullExternalReference;
            bool       lIsRelative;

            if (Path.IsPathRooted(lExternalReference))
            {
                lFullExternalReference = lExternalReference;
                lIsRelative            = false;
            }
            else
            {
                lFullExternalReference = pSerializationContext.CurrentDirectory.FullName + Path.DirectorySeparatorChar + lExternalReference;
                lIsRelative            = true;
            }
            XSerializer lDeserializer = new XSerializer(pSerializationContext.ExternalReferenceResolver);
            object      lResult       = lDeserializer.Deserialize(lFullExternalReference);

            pSerializationContext.ExternalReferenceResolver.RegisterExternalReference(lResult, lFullExternalReference, lIsRelative);
            return(lResult);
        }
Example #7
0
        /// <summary>
        /// Create an instance of the template type.
        /// </summary>
        /// <returns>The instance is the creation succed, default value otherwise.</returns>
        public virtual TObject Create(XSerializer pSerializer)
        {
            try
            {
                if (this.mTemplateNode == null)
                {
                    return(default(TObject));
                }

                if (pSerializer != null)
                {
                    return((TObject)pSerializer.Deserialize(this.mTemplateNode));
                }
                else
                {
                    XSerializer lSerializer = new XSerializer();
                    return((TObject)lSerializer.Deserialize(this.mTemplateNode));
                }
            }
            catch (Exception /*lEx*/)
            {
                return(default(TObject));
            }
        }
        /// <summary>
        /// This method creates the specified element.
        /// </summary>
        /// <param name="pParentElement">The parent element.</param>
        /// <param name="pSerializationContext">The serialization context.</param>
        /// <returns>The created object.</returns>
        public virtual object Create(XElement pParentElement, IXSerializationContext pSerializationContext)
        {
            XAttribute lAttribute = this.mTargetElement.Attribute(XConstants.EXTERNAL_REFERENCE_ATTRIBUTE);
            string lExternalReference = lAttribute.Value;
            string lFullExternalReference;
            bool lIsRelative;
            if (Path.IsPathRooted(lExternalReference))
            {
                lFullExternalReference = lExternalReference;
                lIsRelative = false;
            }
            else
            {
                lFullExternalReference = pSerializationContext.CurrentDirectory.FullName + Path.DirectorySeparatorChar + lExternalReference;
                lIsRelative = true;

            }
            XSerializer lDeserializer = new XSerializer(pSerializationContext.ExternalReferenceResolver);
            object lResult = lDeserializer.Deserialize(lFullExternalReference);
            pSerializationContext.ExternalReferenceResolver.RegisterExternalReference(lResult, lFullExternalReference, lIsRelative);
            return lResult;
        }
Example #9
0
        /// <summary>
        /// Tests the serialization/deserialization of the given object.
        /// </summary>
        /// <param name="pTestName">Name of the test.</param>
        /// <param name="pFirstFilename">The first filename.</param>
        /// <param name="pSecondFilename">The second filename.</param>
        /// <param name="pObject">The object to test.</param>
        /// <returns></returns>
        static TestResult DoTest(string pTestName, string pFirstFilename, string pSecondFilename, object pObject)
        {
            TestResult lResult = new TestResult()
            {
                Name = pTestName, DeserializationPerformanceInfos = new Performance(), SerializationPerformanceInfos = new Performance(), InitPerformanceInfos = new Performance()
            };

            try
            {
                lResult.FirstFilename  = pFirstFilename;
                lResult.SecondFilename = pSecondFilename;
                Stopwatch lSerializationTimer   = new Stopwatch();
                Stopwatch lDeserializationTimer = new Stopwatch();
                Stopwatch lInitTimer            = new Stopwatch();


                // First, serialize the object.
                lSerializationTimer.Restart();
                lInitTimer.Restart();
                XSerializer lFirstWriteSerializer = new XSerializer();
                lInitTimer.Stop();
                lResult.InitPerformanceInfos.AllRuns.Add(lInitTimer.Elapsed);
                XElement lComputedElement = lFirstWriteSerializer.Serialize(pObject);
                lComputedElement.Save(pFirstFilename);
                lSerializationTimer.Stop();
                lResult.SerializationPerformanceInfos.AllRuns.Add(lSerializationTimer.Elapsed);

                // Read the serialized object.
                lDeserializationTimer.Restart();
                lInitTimer.Restart();
                XSerializer lReadSerializer = new XSerializer();
                lInitTimer.Stop();
                lResult.InitPerformanceInfos.AllRuns.Add(lInitTimer.Elapsed);
                object lReadObject = lReadSerializer.Deserialize(pFirstFilename);
                lDeserializationTimer.Stop();
                lResult.DeserializationPerformanceInfos.AllRuns.Add(lDeserializationTimer.Elapsed);

                lSerializationTimer.Restart();
                lInitTimer.Restart();
                XSerializer lSecondWriteSerializer = new XSerializer();
                lInitTimer.Stop();
                lResult.InitPerformanceInfos.AllRuns.Add(lInitTimer.Elapsed);
                XElement lComputedElement0 = lSecondWriteSerializer.Serialize(lReadObject);
                lComputedElement0.Save(pSecondFilename);
                lSerializationTimer.Stop();
                lResult.SerializationPerformanceInfos.AllRuns.Add(lSerializationTimer.Elapsed);

                // Now, compare the text in each file.
                string lFirstContent  = File.ReadAllText(pFirstFilename);
                string lSecondContent = File.ReadAllText(pSecondFilename);
                if (lFirstContent != lSecondContent)
                {
                    lResult.Status = TestStatus.ComparisonFailed;
                }
                else
                {
                    lResult.Status = TestStatus.Sucess;
                }
            }
            catch (Exception lEx)
            {
                lResult.Status    = TestStatus.ExceptionOccured;
                lResult.Exception = lEx;
            }
            return(lResult);
        }