Beispiel #1
0
        /// <summary>
        /// Using reflections to convert XML to class.
        /// </summary>
        /// <param name="strXML">XML to be parsed</param>
        /// <returns></returns>
        public List <ArrayOfResults> convertXmlToClassReflection(string strXML)
        {
            List <ArrayOfResults> lstarr = new List <ArrayOfResults>();

            try
            {
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(strXML);
                var namespaceMgr = new XmlNamespaceManager(xmldoc.NameTable);
                namespaceMgr.AddNamespace("Result", "http://schemas.datacontract.org/2004/07/Rest_BingSearch");
                XmlNode     xmlNode         = xmldoc.SelectSingleNode("//Result:ArrayOfResults", namespaceMgr);
                XmlNodeList xmlResultsChild = xmlNode.ChildNodes;

                ArrayOfResults arrResult = new ArrayOfResults();
                foreach (XmlNode resultNode in xmlResultsChild)
                {
                    DisplayResults disp = new DisplayResults();
                    //Using reflections to get the class property value and convert it into class
                    foreach (XmlNode xml in resultNode.ChildNodes)
                    {
                        disp.GetType().GetProperty("res" + xml.Name).SetValue(disp, xml.InnerText, null);
                    }
                    //Add the result into the list of results.
                    arrResult.lstDisplayRes.Add(disp);
                }
                //In case of multiple array of results
                lstarr.Add(arrResult);
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return(lstarr);
        }