Пример #1
0
        public static T Deserialize <T>(System.Xml.XmlReader reader, bool removeFirstElement)
        {
            T result = default(T);

            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(T));
            if (removeFirstElement)
            {
                if (reader.Read())
                {
                    // Position should be on first element
                    if (reader.Read())
                    {
                        // Position should be on first child element, thereby removing the first element when
                        // the ReadSubtree method is called.
                        if (xs.CanDeserialize(reader.ReadSubtree()))
                        {
                            result = (T)xs.Deserialize(reader.ReadSubtree());
                        }
                    }
                }
            }
            else if (xs.CanDeserialize(reader))
            {
                result = (T)xs.Deserialize(reader);
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// Load L-System definition file
        /// </summary>
        public void LoadDefinition()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".ls";                                           // Default file extension
            dlg.Filter     = "L-System files(*.ls)|*.ls|All files (*.*)|*.*"; // Filter files by extension

            // Show save file dialog box
            bool?result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Load document
                string filename = dlg.FileName;
                try
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(_model.GetType());
                    using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(filename))
                    {
                        if (x.CanDeserialize(reader))
                        {
                            _model = x.Deserialize(reader) as SettingsModel;
                        }
                        reader.Close();
                    }
                }
                catch
                {
                    System.Windows.MessageBox.Show("Couldn't load L-System, please try again later.", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            // update all properties
            InvokePropertyChanged(null);
        }
Пример #3
0
        /// <summary>
        ///  Deserializes the XML content to a specified object
        /// </summary>
        /// <param name="xml">
        /// <returns></returns>
        public T Deserialize()

        {
            string xml = String.Empty;

            var fileStream = new FileStream(@"D:\\DotNET\\test\\CaseInstaller.xml", FileMode.Open, FileAccess.Read);

            using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
            {
                xml = streamReader.ReadToEnd();
            }

            T result = default(T);

            if (!string.IsNullOrEmpty(xml))
            {
                System.IO.TextReader textreader = new System.IO.StringReader(xml);
                System.Xml.XmlReader xmlreader  = System.Xml.XmlReader.Create(textreader);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

                if (serializer.CanDeserialize(xmlreader))
                {
                    result = (T)serializer.Deserialize(xmlreader);
                }
            }

            return(result);
        }
Пример #4
0
        /// <summary>
        /// این متد یک فولدر را می گیرد و لیست فایلها و یا نام فایل هائی که قابل دسترس هستند را باز می گرداند
        /// </summary>
        public static List <string> GetBooksOfFolder(string folderPath, bool onlyFileName)
        {
            System.IO.StreamReader objStream = null;
            try
            {
                List <string> returnValue = new List <string>();
                System.Xml.Serialization.XmlSerializer objSerializer = new System.Xml.Serialization.XmlSerializer(typeof(List <GoodSpeechOfBigMan>));
                System.Xml.XmlReader objXMLReader;

                string[] aryFile = System.IO.Directory.GetFiles(folderPath);
                foreach (string itemFileName in aryFile)
                {
                    objStream    = new System.IO.StreamReader(itemFileName);
                    objXMLReader = System.Xml.XmlReader.Create(objStream);
                    if (objSerializer.CanDeserialize(objXMLReader))
                    {
                        if (onlyFileName)
                        {
                            returnValue.Add(System.IO.Path.GetFileNameWithoutExtension(itemFileName));
                        }
                        else
                        {
                            returnValue.Add(itemFileName);
                        }
                    }
                }
                return(returnValue);
            }
            catch (System.Exception ex)
            {
                System.Exception newEx = new System.Exception("برنامه هنگام شناسائی لیست کتاب های جملات زیبا دچار خطا شده است", ex);
                newEx.Data.Add(SeifaeiBrothersException.cntKeyNameExceptionID, "6C178EE4-8DD5-43CD-BEFA-3C3A9AB464CB");
                throw newEx;
            }
        }
Пример #5
0
 public bool TryGetTypeGeneral(string value, out object result)
 {
     System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(value.GetType());
     using (System.IO.TextReader tr = new System.IO.StringReader(value))
     {
         System.Xml.XmlReader xr;
         try {
             xr = System.Xml.XmlReader.Create(tr);
         }
         catch
         {
             result = default(T);
             return(false);
         }
         if (xs.CanDeserialize(xr))
         {
             result = (T)xs.Deserialize(xr);
             return(true);
         }
         else
         {
             result = default(T);
             return(false);
         }
     }
 }
 static public string[] NameAndCasNmber(string compoundName)
 {
     string[] retVal = new string[2];
     gov.nih.nlm.chemspell.SpellAidService service = new gov.nih.nlm.chemspell.SpellAidService();
     string response = service.getSugList(compoundName, "All databases");
     var XMLReader = new System.Xml.XmlTextReader(new System.IO.StringReader(response));
     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Synonym));
     if (serializer.CanDeserialize(XMLReader))
     {
         Synonym synonym = (Synonym)serializer.Deserialize(XMLReader);
         foreach (SynonymChemical chemical in synonym.Chemical)
         {
             int result = String.Compare(compoundName, chemical.Name, true);
             if (result == 0)
             {
                 retVal[0] = chemical.CAS;
                 retVal[1] = chemical.Name;
                 return retVal;
             }
         }
     }
     serializer = new System.Xml.Serialization.XmlSerializer(typeof(SpellAid));
     if (serializer.CanDeserialize(XMLReader))
     {
         SpellAid aid = (SpellAid)serializer.Deserialize(XMLReader);
         bool different = true;
         retVal[0] = aid.Chemical[0].CAS;
         retVal[1] = aid.Chemical[0].Name;
         for (int i = 0; i < aid.Chemical.Length - 1; i++)
         {
             if (retVal[0] != aid.Chemical[i + 1].CAS)
             {
                 different = false;
                 retVal[0] = aid.Chemical[i].CAS;
                 retVal[1] = aid.Chemical[i].Name;
             }
         }
         if (!different)
         {
             foreach (SpellAidChemical chemical in aid.Chemical)
             {
                 int result = String.Compare(compoundName, 0, chemical.Name, 0, compoundName.Length, true);
                 if (result == 0 && compoundName.Length >= chemical.Name.Length)
                 {
                     retVal[0] = chemical.CAS;
                     retVal[1] = chemical.Name;
                     return retVal;
                 }
             }
             SelectChemicalForm form = new SelectChemicalForm(aid, compoundName);
             form.ShowDialog();
             retVal[0] = form.SelectedChemical.CAS;
             retVal[1] = form.SelectedChemical.Name;
             return retVal;
         }
     }
     return retVal;
 }
Пример #7
0
        public static T Deserialize(XmlReader reader)
        {
            if (!_serializer.CanDeserialize(reader))
            {
                throw new InvalidDataException("Failed to read Xml.");
            }

            return((T)_serializer.Deserialize(reader));
        }
Пример #8
0
        public static T Deserialize <T>(string xml)
        {
            T result = default(T);

            if (!string.IsNullOrEmpty(xml))
            {
                System.IO.TextReader tr     = new System.IO.StringReader(xml);
                System.Xml.XmlReader reader = System.Xml.XmlReader.Create(tr);
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));

                if (serializer.CanDeserialize(reader))
                {
                    result = (T)serializer.Deserialize(reader);
                }
            }
            return(result);
        }
Пример #9
0
    public static Preferences Load()
    {
        Preferences pref = null;

        if (File.Exists(Preferences.FileFullPath))
        {
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Preferences));
            using (var xmlReader = new System.Xml.XmlTextReader(Preferences.FileFullPath))
            {
                if (serializer.CanDeserialize(xmlReader))
                {
                    pref = serializer.Deserialize(xmlReader) as Preferences;
                }
            }
        }
        return((pref == null) ? new Preferences() : pref);
    }
Пример #10
0
 /// <summary>
 /// XMLファイル読込み
 /// </summary>
 /// <typeparam name="T">クラス</typeparam>
 /// <param name="readPath">読込パス</param>
 /// <param name="readData">デシリアライズされたクラス</param>
 /// <returns>読込み正否</returns>
 public static Boolean TryLoad <T>(String readPath, out T readData) where T : class
 {
     readData = default(T);
     try
     {
         // 読み込み用オブジェ作成
         var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
         // 読込み
         using (var sr = new StreamReader(readPath, new UTF8Encoding(false)))
         {   // デシリアライズできるか判定を設ける
             if (serializer.CanDeserialize(new XmlTextReader(sr)))
             {
                 readData = serializer.Deserialize(sr) as T;
                 return(true);    // 読込み成功時にはTrue
             }
         }
     }
     catch (Exception) { return(false); }
     return(false);// 何も無くきた場合は、読込できなかったとしてfalse
 }
Пример #11
0
        /// <summary>
        /// Load embedded example
        /// </summary>
        /// <param name="name">Example file name</param>
        public void LoadExample(string name)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(_model.GetType());
                using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(System.Windows.Application.GetResourceStream(new System.Uri("/examples/" + name, UriKind.Relative)).Stream))
                {
                    if (x.CanDeserialize(reader))
                    {
                        _model = x.Deserialize(reader) as SettingsModel;
                    }
                    reader.Close();
                }

                // update all properties
                InvokePropertyChanged(null);
            }
            catch
            {
                System.Windows.MessageBox.Show("Couldn't load L-System, please try again later.", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
Пример #12
0
 private void findCompound(ref string compoundName, ref string CasNo)
 {
     gov.nih.nlm.chemspell.SpellAidService service = new gov.nih.nlm.chemspell.SpellAidService();
     string response = service.getSugList(compoundName, "All databases");
     response = response.Replace("&", "&amp;");
     var XMLReader = new System.Xml.XmlTextReader(new System.IO.StringReader(response));
     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Synonym));
     if (serializer.CanDeserialize(XMLReader))
     {
         // Synonyms means more than one name for the same chemical/CAS Number.
         Synonym synonym = (Synonym)serializer.Deserialize(XMLReader);
         CasNo = synonym.Chemical[0].CAS;
         this.listBox1.BeginUpdate();
         foreach (SynonymChemical chemical in synonym.Chemical)
         {
             this.listBox1.Items.Add(chemical.Name);
             if (CasNo != chemical.CAS)
             {
                 System.Windows.Forms.MessageBox.Show(compoundName + " has a synonym with a different CAS Number.");
                 return;
             }
         }
         this.listBox1.EndUpdate();
         return;
     }
     serializer = new System.Xml.Serialization.XmlSerializer(typeof(SpellAid));
     if (serializer.CanDeserialize(XMLReader))
     {
         SpellAid aid = (SpellAid)serializer.Deserialize(XMLReader);
         Form3 selector = new Form3();
         selector.chemicals = aid;
         selector.ShowDialog();
         compoundName = selector.SelectedChemical;
         this.findCompound(ref compoundName, ref CasNo);
         return;
     }
     CasNo = string.Empty;
 }
Пример #13
0
        /// <summary>
        /// Load embedded example
        /// </summary>
        /// <param name="name">Example file name</param>
        public void LoadExample(string name)
        {
            try
            {
                System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(_model.GetType());
                using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(System.Windows.Application.GetResourceStream(new System.Uri("/examples/" + name, UriKind.Relative)).Stream))
                {
                    if (x.CanDeserialize(reader))
                        _model = x.Deserialize(reader) as SettingsModel;
                    reader.Close();
                }

                // update all properties
                InvokePropertyChanged(null);
            }
            catch
            {
                System.Windows.MessageBox.Show("Couldn't load L-System, please try again later.", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
Пример #14
0
        /// <summary>
        /// Load L-System definition file
        /// </summary>
        public void LoadDefinition()
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.DefaultExt = ".ls"; // Default file extension
            dlg.Filter = "L-System files(*.ls)|*.ls|All files (*.*)|*.*"; // Filter files by extension

            // Show save file dialog box
            bool? result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Load document
                string filename = dlg.FileName;
                try
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(_model.GetType());
                    using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(filename))
                    {
                        if (x.CanDeserialize(reader))
                            _model = x.Deserialize(reader) as SettingsModel;
                        reader.Close();
                    }
                }
                catch
                {
                    System.Windows.MessageBox.Show("Couldn't load L-System, please try again later.", "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }

            // update all properties
            InvokePropertyChanged(null);
        }
Пример #15
0
        private void button2_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Visible = false;
            compoundName = this.textBox1.Text;
            casNo = NISTChemicalList.casNumber(compoundName);
            if (string.IsNullOrEmpty(casNo))
            {
                gov.nih.nlm.chemspell.SpellAidService service = new gov.nih.nlm.chemspell.SpellAidService();
                string response = service.getSugList(compoundName, "All databases");
                var XMLReader = new System.Xml.XmlTextReader(new System.IO.StringReader(response));
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Synonym));
                if (serializer.CanDeserialize(XMLReader))
                {
                    // Synonyms means more than one name for the same chemical/CAS Number.
                    Synonym synonym = (Synonym)serializer.Deserialize(XMLReader);
                    casNo = synonym.Chemical[0].CAS;
                    foreach (SynonymChemical chemical in synonym.Chemical)
                    {
                        if (casNo != chemical.CAS)
                        {
                            System.Windows.Forms.MessageBox.Show(compoundName + "has a synonym with a different CAS Number.");
                            return;
                        }
                    }
                    label2.Text = "The CAS Number of the chemical is: " + casNo + ". Now select the finished button to exit.";
                    return;
                }
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(SpellAid));
                if (serializer.CanDeserialize(XMLReader))
                {
                    SpellAid aid = (SpellAid)serializer.Deserialize(XMLReader);
                    //bool different = true;
                    chemicalList = new List<SpellAidChemical>();
                    foreach (SpellAidChemical chemical in aid.Chemical)
                    {
                        chemicalList.Add(chemical);
                    }
                    this.dataGridView1.DataSource = chemicalList;
                    this.dataGridView1.Visible = true;
                    this.label2.Text = "Select the desired chemical from the list below and click the 'Finished' button to exit.";
                    return;

                    //    retVal[0] = aid.Chemical[0].CAS;
                    //    retVal[1] = aid.Chemical[0].Name;
                    //    for (int i = 0; i < aid.Chemical.Length - 1; i++)
                    //    {
                    //        if (retVal[0] != aid.Chemical[i + 1].CAS)
                    //        {
                    //            different = false;
                    //            retVal[0] = aid.Chemical[i].CAS;
                    //            retVal[1] = aid.Chemical[i].Name;
                    //        }
                    //    }
                    //    if (!different)
                    //    {
                    //        foreach (SpellAidChemical chemical in aid.Chemical)
                    //        {
                    //            int result = String.Compare(compoundName, 0, chemical.Name, 0, compoundName.Length, true);
                    //            if (result == 0 && compoundName.Length >= chemical.Name.Length)
                    //            {
                    //                retVal[0] = chemical.CAS;
                    //                retVal[1] = chemical.Name;
                    //                return retVal;
                    //            }
                    //        }
                    //        SelectChemicalForm form = new SelectChemicalForm(aid, compoundName);
                    //        form.ShowDialog();
                    //        retVal[0] = form.SelectedChemical.CAS;
                    //        retVal[1] = form.SelectedChemical.Name;
                    //        return retVal;
                    //    }
                }
            }
            label2.Text = "The CAS Number of the chemical is: " + casNo + ". Now select the 'Finished' button to exit.";
            return;
        }
        /// <summary>
        /// Perform an upsert operation by either creating a contact or merging
        /// the given data into all contacts that match the provided merge
        /// column. For example, if the email field is provided in the fields
        /// parameter and is also specified as the merge column, all contacts
        /// with that email value will have their entries updated with the new
        /// data. If there is no match, a new contact will be created.
        /// </summary>
        /// <param name="fields">The fields to create / update.</param>
        /// <param name="mergeColumnID">The column ID to merge upon.</param>
        /// <returns>A collection containing all contacts modified by the
        /// <param name="listIDs">An array of list IDs to add the contact(s) to.
        /// </param>
        /// operation.</returns>
        public Responses.ContactCollection UpsertContact(Dictionary <string, string> fields, string mergeColumnID, int[] listIDs)
        {
            Dictionary <string, string> queryParameters = new Dictionary <string, string>();

            queryParameters.Add("mergecolumn", mergeColumnID);

            Requests.ContactEntity contact = new Requests.ContactEntity();

            Requests.FieldEntity[] fieldArray = new Requests.FieldEntity[fields.Count];
            int fieldCounter = 0;

            foreach (KeyValuePair <string, string> f in fields)
            {
                Requests.FieldEntity fieldEntity = new Requests.FieldEntity();
                fieldEntity.ID             = f.Key;
                fieldEntity.Value          = f.Value;
                fieldArray[fieldCounter++] = fieldEntity;
            }
            contact.Fields = fieldArray;

            if (listIDs.Length > 0)
            {
                int listCounter = 0;
                Requests.ListEntity[] listArray = new Requests.ListEntity[listIDs.Length];
                foreach (int id in listIDs)
                {
                    Requests.ListEntity listEntity = new Requests.ListEntity();
                    listEntity.ID            = id;
                    listArray[listCounter++] = listEntity;
                }
                contact.ListIds = listArray;
            }

            contact.ID = null;

            string xml = this.connection.Call <string>("POST", "contactmanager/contacts", queryParameters, contact);

            //We're going to deserialize ourselves, since the response could be
            //either a contact collection or a contact entity. We're going to
            //turn the contact entity into a collection for consistency.
            using (System.Xml.XmlReader reader = new System.Xml.XmlTextReader(new System.IO.StringReader(xml)))
            {
                //If it's a collection, return it.
                System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Responses.ContactCollection));
                if (serializer.CanDeserialize(reader))
                {
                    return((Responses.ContactCollection)serializer.Deserialize(reader));
                }

                serializer = new System.Xml.Serialization.XmlSerializer(typeof(Responses.ContactEntity));

                //If it's an entity, create a collection and put the contact in it.
                if (serializer.CanDeserialize(reader))
                {
                    Responses.ContactEntity     c = (Responses.ContactEntity)serializer.Deserialize(reader);
                    Responses.ContactCollection contactCollection = new Responses.ContactCollection();

                    contactCollection.Contacts = new Responses.ContactEntity[] { (Responses.ContactEntity)c };
                    return(contactCollection);
                }
                else
                {
                    throw new Exception("Invalid XML response: could not deserialize into a ContactEntity or ContactCollection.");
                }
            }
        }
Пример #17
0
 private void ToxnetHSDBButton_Click_1(object sender, EventArgs e)
 {
     string uriString = "https://toxgate.nlm.nih.gov/cgi-bin/sis/search2";
     System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriString);
     request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriString);
     string postData = "queryxxx=" + m_casNo;
     postData += "&chemsyn=1";
     postData += "&database=hsdb";
     postData += "&Stemming=1";
     postData += "&and=1";
     postData += "&second_search=1";
     postData += "&gateway=1";
     var data = Encoding.ASCII.GetBytes(postData);
     request.Method = "POST";
     request.ContentType = "application/x-www-form-urlencoded";
     request.ContentLength = data.Length;
     using (var stream = request.GetRequestStream())
     {
         stream.Write(data, 0, data.Length);
     }
     System.Net.WebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
     string responseString = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
     string s1 = responseString.Replace("<br>", "");
     var XMLReader = new System.Xml.XmlTextReader(new System.IO.StringReader(s1));
     System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(QueryResult));
     if (serializer.CanDeserialize(XMLReader))
     {
         // Synonyms means more than one name for the same chemical/CAS Number.
         QueryResult result = (QueryResult)serializer.Deserialize(XMLReader);
         uriString = "https://toxgate.nlm.nih.gov/cgi-bin/sis/search2/f?" + result.TemporaryFile;
         string[] ids = result.Id.Split(' ');
         System.Diagnostics.Process.Start("http://toxgate.nlm.nih.gov/cgi-bin/sis/search2/r?dbs+hsdb:@term+@DOCNO+" + ids[0]);
     }
 }