Wednesday, 14 December 2011

XML Parse

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using Universal.Gateway.Business.Providers;
namespace Universal.Gateway.Business
{
    // MethodsProvider
    public class ParseXML
    {
        #region  Variables region
        public string ElementName { get; set; }
        public string AttributeName { get; set; }
        public string Value { get; set; }
        List<ParseXML> LstResult;
        #endregion

        #region Constructor region
        public ParseXML()
        {
            ElementName = AttributeName = Value = String.Empty;
            LstResult = new List<ParseXML>();
        }
        #endregion

        #region "Private Functions region"
        private List<ParseXML> ParseXMLInput(XDocument xmlDoc)
        {
            #region Commented code for checking the output

            // \"
            //            pXML = @"<?xml version = ""1.0"" encoding = ""UTF-8""?>
            //<s2s:s2sMessage
            //xmlns:s2s = ""http://www.gamingstandards.com/s2s/schemas/v1.2.6/""
            //xmlns:pli = ""http://www.gamingstandards.com/s2s/schemas/v1.2.6/pli"">
            //<s2s:s2sHeader s2s:applicationId = ""20"" s2s:transactionId = ""5"" s2s:toSystem = ""http://10.128.58.90:8732/S2SResponseHandler/""
            //s2s:fromSystem = ""http://10.147.15.223:8080/ListenerService.asmx ""
            //s2s:messageId = ""1812344""
            //s2s:referenceMessageId = """"
            //s2s:dateTimeSent = ""2009-03-18T15:24:39.123-05:00""/>
            //<s2s:s2sBody>
            //  <pli:playerInfo
            //       s2s:propertyId=""I5""
            //       s2s:gameDate=""2011-08-25T14:00:26.2517768-07:00""
            //       s2s:dateTime = ""2009-03-18T15:24:39.012-05:00""
            //       s2s:commandId = ""1812344""
            //       s2s:timeToLive = ""3000""
            //       s2s:sessionType = ""request""
            //       s2s:sessionId = ""10""
            //       s2s:sessionRetry = ""2""
            //       s2s:retryCount = ""3""
            //       s2s:clientType = ""S2S_system""
            //       s2s:clientId = ""ABC_clientSystem""
            //       s2s:transaction = """"
            //       s2s:transDateTime = ""2011-08-25T14:00:26.2517768-07:00""
            //       s2s:transStation = """"
            //       s2s:transEmployee =""47474""
            //       s2s:transApproved = """">
            //    <pli:getPlayerBalances
            //       pli:playerId = ""I5000101977""
            //       pli:includeAll = ""Y"" ></pli:getPlayerBalances>
            //    </pli:playerInfo>
            //  </s2s:s2sBody>
            //</s2s:s2sMessage>";

            #endregion
            if (xmlDoc != null)
            {
                try
                {
                    LstResult = new List<ParseXML>();
                    List<ParseXML> LstTempResultAttributes = new List<ParseXML>();
                    var ddd = from c in xmlDoc.Descendants().Elements()
                              select c;

                    foreach (var Mainitem in ddd)
                    {
                        if (Mainitem.HasAttributes == false)
                        {
                            if (Mainitem.HasElements)
                            {
                                foreach (var itemElement in Mainitem.Elements())
                                {
                                    #region itemElement Attributes region
                                    if (itemElement.HasAttributes)
                                    {
                                        LstTempResultAttributes = new List<ParseXML>();
                                        LstTempResultAttributes = (from abc in itemElement.Attributes()
                                                                   select new ParseXML { ElementName = itemElement.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                                    }

                                    foreach (var tempitem in LstTempResultAttributes)
                                    {
                                        List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                             where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                             select abc).ToList();

                                        if (LstCheckDuplicates.Count <= 0)
                                        {
                                            LstResult.Add(tempitem);
                                        }

                                    }

                                    if (itemElement.HasAttributes == false)
                                    {
                                        List<ParseXML> LstCheckDuplicatesElements = (from abc in LstResult
                                                                                     where abc.AttributeName == Mainitem.Name.LocalName && abc.ElementName == itemElement.Name.LocalName && abc.Value == itemElement.Value
                                                                                     select abc).ToList();

                                        if (LstCheckDuplicatesElements.Count <= 0)
                                        {
                                            LstResult.Add(new ParseXML { ElementName = Mainitem.Name.LocalName, AttributeName = itemElement.Name.LocalName, Value = itemElement.Value });
                                        }
                                    }
                                    #endregion


                                    if (itemElement.HasElements)
                                    {
                                        foreach (var itemSubElemenet in itemElement.Elements())
                                        {
                                            #region SubElement region
                                            if (itemSubElemenet.HasElements)
                                            {

                                                foreach (var itemsub2Element in itemSubElemenet.Elements())
                                                {
                                                    #region Sub2Element Region
                                                    #region SubElement Attributes region
                                                    if (itemsub2Element.HasAttributes)
                                                    {
                                                        LstTempResultAttributes = new List<ParseXML>();
                                                        LstTempResultAttributes = (from abc in itemsub2Element.Attributes()
                                                                                   select new ParseXML { ElementName = itemsub2Element.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                                                    }

                                                    foreach (var tempitem in LstTempResultAttributes)
                                                    {
                                                        List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                                             where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                                             select abc).ToList();

                                                        if (LstCheckDuplicates.Count <= 0)
                                                        {
                                                            LstResult.Add(tempitem);
                                                        }

                                                    }
                                                    #endregion


                                                    #region Sub2Element Elements region
                                                    if (itemsub2Element.HasElements)
                                                    {
                                                        List<ParseXML> LstCheckDuplicatesSub2Elements = (from abc in LstResult
                                                                                                         where abc.AttributeName == itemSubElemenet.Name.LocalName && abc.ElementName == itemsub2Element.Name.LocalName && abc.Value == itemsub2Element.Value
                                                                                                         select abc).ToList();
                                                        if (LstCheckDuplicatesSub2Elements.Count <= 0)
                                                        {
                                                            LstResult.Add(new ParseXML { ElementName = itemSubElemenet.Name.LocalName, AttributeName = itemsub2Element.Name.LocalName, Value = itemsub2Element.Value });
                                                        }
                                                    }
                                                    #endregion
                                                    #endregion
                                                }


                                                List<ParseXML> LstCheckDuplicatesSubElements = (from abc in LstResult
                                                                                                where abc.AttributeName == itemElement.Name.LocalName && abc.ElementName == itemSubElemenet.Name.LocalName && abc.Value == itemSubElemenet.Value
                                                                                                select abc).ToList();
                                                if (LstCheckDuplicatesSubElements.Count <= 0)
                                                {
                                                    LstResult.Add(new ParseXML { ElementName = itemElement.Name.LocalName, AttributeName = itemSubElemenet.Name.LocalName, Value = itemSubElemenet.Value });
                                                }
                                            }
                                            #endregion
                                        }
                                    }


                                }

                            }


                        }

                        if (Mainitem.HasAttributes)
                        {
                            LstTempResultAttributes = new List<ParseXML>();
                            LstTempResultAttributes = (from abc in Mainitem.Attributes()
                                                       select new ParseXML { ElementName = Mainitem.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                        }

                        foreach (var tempitem in LstTempResultAttributes)
                        {
                            List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                 where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                 select abc).ToList();

                            if (LstCheckDuplicates.Count <= 0)
                            {
                                LstResult.Add(tempitem);
                            }

                        }


                    }





                }
                catch (Exception ex)
                {

                    throw ex;
                }

            }

            return LstResult;
        }


        private List<ParseXML> ParseXMLInputV2(XDocument xmlDoc)
        {
            #region Commented code for checking the output

            // \"
            //            pXML = @"<?xml version = ""1.0"" encoding = ""UTF-8""?>
            //<s2s:s2sMessage
            //xmlns:s2s = ""http://www.gamingstandards.com/s2s/schemas/v1.2.6/""
            //xmlns:pli = ""http://www.gamingstandards.com/s2s/schemas/v1.2.6/pli"">
            //<s2s:s2sHeader s2s:applicationId = ""20"" s2s:transactionId = ""5"" s2s:toSystem = ""http://10.128.58.90:8732/S2SResponseHandler/""
            //s2s:fromSystem = ""http://10.147.15.223:8080/ListenerService.asmx ""
            //s2s:messageId = ""1812344""
            //s2s:referenceMessageId = """"
            //s2s:dateTimeSent = ""2009-03-18T15:24:39.123-05:00""/>
            //<s2s:s2sBody>
            //  <pli:playerInfo
            //       s2s:propertyId=""I5""
            //       s2s:gameDate=""2011-08-25T14:00:26.2517768-07:00""
            //       s2s:dateTime = ""2009-03-18T15:24:39.012-05:00""
            //       s2s:commandId = ""1812344""
            //       s2s:timeToLive = ""3000""
            //       s2s:sessionType = ""request""
            //       s2s:sessionId = ""10""
            //       s2s:sessionRetry = ""2""
            //       s2s:retryCount = ""3""
            //       s2s:clientType = ""S2S_system""
            //       s2s:clientId = ""ABC_clientSystem""
            //       s2s:transaction = """"
            //       s2s:transDateTime = ""2011-08-25T14:00:26.2517768-07:00""
            //       s2s:transStation = """"
            //       s2s:transEmployee =""47474""
            //       s2s:transApproved = """">
            //    <pli:getPlayerBalances
            //       pli:playerId = ""I5000101977""
            //       pli:includeAll = ""Y"" ></pli:getPlayerBalances>
            //    </pli:playerInfo>
            //  </s2s:s2sBody>
            //</s2s:s2sMessage>";

            #endregion
            if (xmlDoc != null)
            {
                try
                {
                    LstResult = new List<ParseXML>();
                    List<ParseXML> LstTempResultAttributes = new List<ParseXML>();
                   
                   
                    var ddd = from c in xmlDoc.Descendants().Elements()
                              select c;

                    foreach (var Mainitem in ddd)
                    {
                        if (Mainitem.HasAttributes == false)
                        {
                            if (Mainitem.HasElements)
                            {
                                foreach (var itemElement in Mainitem.Elements())
                                {
                                    #region itemElement Attributes region
                                    if (itemElement.HasAttributes)
                                    {
                                        LstTempResultAttributes = new List<ParseXML>();
                                        LstTempResultAttributes = (from abc in itemElement.Attributes()
                                                                   select new ParseXML { ElementName = itemElement.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                                    }

                                    foreach (var tempitem in LstTempResultAttributes)
                                    {
                                        List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                             where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                             select abc).ToList();

                                        if (LstCheckDuplicates.Count <= 0)
                                        {
                                            LstResult.Add(tempitem);
                                        }

                                    }

                                    if (itemElement.HasAttributes == false)
                                    {
                                        List<ParseXML> LstCheckDuplicatesElements = (from abc in LstResult
                                                                                     where abc.AttributeName == Mainitem.Name.LocalName && abc.ElementName == itemElement.Name.LocalName && abc.Value == itemElement.Value
                                                                                     select abc).ToList();

                                        if (LstCheckDuplicatesElements.Count <= 0)
                                        {
                                            LstResult.Add(new ParseXML { ElementName = Mainitem.Name.LocalName, AttributeName = itemElement.Name.LocalName, Value = itemElement.Value });
                                        }
                                    }
                                    #endregion


                                    if (itemElement.HasElements)
                                    {
                                        foreach (var itemSubElemenet in itemElement.Elements())
                                        {
                                            #region SubElement region
                                            if (itemSubElemenet.HasElements)
                                            {

                                                foreach (var itemsub2Element in itemSubElemenet.Elements())
                                                {
                                                    #region Sub2Element Region
                                                    #region SubElement Attributes region
                                                    if (itemsub2Element.HasAttributes)
                                                    {
                                                        LstTempResultAttributes = new List<ParseXML>();
                                                        LstTempResultAttributes = (from abc in itemsub2Element.Attributes()
                                                                                   select new ParseXML { ElementName = itemsub2Element.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                                                    }

                                                    foreach (var tempitem in LstTempResultAttributes)
                                                    {
                                                        List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                                             where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                                             select abc).ToList();

                                                        if (LstCheckDuplicates.Count <= 0)
                                                        {
                                                            LstResult.Add(tempitem);
                                                        }

                                                    }
                                                    #endregion


                                                    #region Sub2Element Elements region
                                                    if (itemsub2Element.HasElements)
                                                    {
                                                        //List<ParseXML> LstCheckDuplicatesSub2Elements = (from abc in LstResult
                                                        //                                                 where abc.AttributeName == itemSubElemenet.Name.LocalName && abc.ElementName == itemsub2Element.Name.LocalName && abc.Value == itemsub2Element.Value
                                                        //                                                 select abc).ToList();
                                                        //if (LstCheckDuplicatesSub2Elements.Count <= 0)
                                                        //{
                                                        //    LstResult.Add(new ParseXML { ElementName = itemSubElemenet.Name.LocalName, AttributeName = itemsub2Element.Name.LocalName, Value = itemsub2Element.Value });
                                                        //}

                                                        foreach (var Sub3Element in itemsub2Element.Elements())
                                                        {

                                                            if (Sub3Element.HasElements)
                                                            {
                                                                foreach (var Sub4Element in Sub3Element.Elements())
                                                                {
                                                                    List<ParseXML> LstCheckDuplicatesSub3Elements = (from abc in LstResult
                                                                                                                     where abc.AttributeName == Sub4Element.Name.LocalName && abc.ElementName == Sub3Element.Name.LocalName && abc.Value == Sub3Element.Value
                                                                                                                     select abc).ToList();

                                                                    if (LstCheckDuplicatesSub3Elements.Count <= 0)
                                                                    {
                                                                        LstResult.Add(new ParseXML { ElementName = Sub4Element.Name.LocalName, AttributeName = Sub3Element.Name.LocalName, Value = Sub3Element.Value });
                                                                    }
                                                                }
                                                            }



                                                            List<ParseXML> LstCheckDuplicatesSub2Elements = (from abc in LstResult
                                                                                                             where abc.AttributeName == itemsub2Element.Name.LocalName && abc.ElementName == Sub3Element.Name.LocalName && abc.Value == Sub3Element.Value
                                                                                                             select abc).ToList();



                                                            if (LstCheckDuplicatesSub2Elements.Count <= 0)
                                                            {
                                                                LstResult.Add(new ParseXML { ElementName = itemsub2Element.Name.LocalName, AttributeName = Sub3Element.Name.LocalName, Value = Sub3Element.Value });
                                                            }
                                                        }
                                                    }
                                                    #endregion
                                                    #endregion
                                                }


                                                List<ParseXML> LstCheckDuplicatesSubElements = (from abc in LstResult
                                                                                                where abc.AttributeName == itemElement.Name.LocalName && abc.ElementName == itemSubElemenet.Name.LocalName && abc.Value == itemSubElemenet.Value
                                                                                                select abc).ToList();
                                                if (LstCheckDuplicatesSubElements.Count <= 0)
                                                {
                                                    LstResult.Add(new ParseXML { ElementName = itemElement.Name.LocalName, AttributeName = itemSubElemenet.Name.LocalName, Value = itemSubElemenet.Value });
                                                }
                                            }
                                            #endregion
                                        }
                                    }


                                }

                            }


                        }

                        if (Mainitem.HasAttributes)
                        {
                            LstTempResultAttributes = new List<ParseXML>();
                            LstTempResultAttributes = (from abc in Mainitem.Attributes()
                                                       select new ParseXML { ElementName = Mainitem.Name.LocalName, AttributeName = abc.Name.LocalName, Value = abc.Value }).ToList();
                        }

                        foreach (var tempitem in LstTempResultAttributes)
                        {
                            List<ParseXML> LstCheckDuplicates = (from abc in LstResult
                                                                 where abc.AttributeName == tempitem.AttributeName && abc.ElementName == tempitem.ElementName && abc.Value == tempitem.Value
                                                                 select abc).ToList();

                            if (LstCheckDuplicates.Count <= 0)
                            {
                                LstResult.Add(tempitem);
                            }

                        }


                    }





                }
                catch (Exception ex)
                {

                    throw ex;
                }

            }

            return LstResult;
        }
        #endregion

        #region  "Public Functions region"

        public String GetXMLResult(XDocument pXDocument, String pElementName, String pAttributeName)
        {

            String StrResult = String.Empty;
            try
            {
                List<ParseXML> LstParseXML = ParseXMLInput(pXDocument);
                List<ParseXML> Lstresult = (from abc in LstParseXML
                                              where abc.ElementName.ToLower().Trim().Contains(pElementName.Trim().ToLower()) && abc.AttributeName.ToLower().Trim().Contains(pAttributeName.Trim().ToLower())
                                            select abc).ToList();

                if (Lstresult != null && Lstresult.Count > 0)
                {
                    StrResult = Lstresult[0].Value;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return StrResult;

        }

        /// <summary>
        /// Get namespace and then find the version number
        /// </summary>
        /// <param name="pXDocument"></param>
        /// <param name="pAttributeName"></param>
        /// <returns></returns>
        public string GetRootNamespaces(XDocument pXDocument, string pAttributeName = "")
        {
            string getMessageVersion = "";
            var result = (from bb in pXDocument.Root.Attributes()
                          //where bb.Name.LocalName.ToLower() == pAttributeName.ToLower()
                          select bb.Value).FirstOrDefault().ToString();

            if (result != null)
            {
                string[] version = result.Split('/');
                foreach (string s in version)
                {
                    if (s.StartsWith("v"))
                    {
                        getMessageVersion = s.Replace("v", "");
                        break;
                    }
                }
            }
            return getMessageVersion ?? string.Empty;
        }


        public String GetElementValue(XDocument pXDocument, String pElementName, String pAttributeName)
        {
            String StrResult = String.Empty;
            try
            {
                List<ParseXML> LstParseXML = ParseXMLInput(pXDocument);
                List<ParseXML> Lstresult = (from abc in LstParseXML
                                            where abc.ElementName.ToLower().Trim().Contains(pElementName.Trim().ToLower()) && abc.AttributeName.ToLower().Trim().Contains(pAttributeName.Trim().ToLower())
                                            select abc).ToList();

                if (Lstresult != null && Lstresult.Count > 0)
                {
                    StrResult = Lstresult[0].Value;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return StrResult;

        }

        public String GetAttributeValue(XDocument pXDocument,string attributeName)
        {
            String StrResult = String.Empty;
            try
            {
                List<ParseXML> LstParseXML = ParseXMLInput(pXDocument);
                List<ParseXML> Lstresult = (from abc in LstParseXML
                                            where abc.AttributeName == attributeName
                                            select abc).ToList();

                if (Lstresult != null && Lstresult.Count > 0)
                {
                    StrResult = Lstresult[0].Value;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return StrResult;
        }
      
        public String GetPropertyID(XDocument pXDocument)
        {
            String StrResult = String.Empty;
            try
            {
                List<ParseXML> LstParseXML = ParseXMLInput(pXDocument);
                List<ParseXML> Lstresult = (from abc in LstParseXML
                                            where abc.AttributeName == "propertyId"
                                            select abc).ToList();

                if (Lstresult != null && Lstresult.Count > 0)
                {
                    StrResult = Lstresult[0].Value;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return StrResult;
        }


        public String GetElementValue(XDocument pXDocument, List<ParseXML> LstParseXML, String pElementName, String pAttributeName)
        {
            String StrResult = String.Empty;
            try
            {
                if (LstParseXML != null && LstParseXML.Count > 0)
                {
                    List<ParseXML> Lstresult = (from abc in LstParseXML
                                                where abc.ElementName.ToLower().Trim().Contains(pElementName.Trim().ToLower()) && abc.AttributeName.ToLower().Trim().Contains(pAttributeName.Trim().ToLower())
                                                select abc).ToList();

                    if (Lstresult != null && Lstresult.Count > 0)
                    {
                        StrResult = Lstresult[0].Value;
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return StrResult;

        }

        public List<ParseXML> GetXMLResultLIst(XDocument pXDocument, Int32 Version)
        {
            List<ParseXML> LstParseXML = null;
            if (pXDocument != null)
            {
                try
                {
                    if (Version == 1)
                    {
                        LstParseXML = ParseXMLInput(pXDocument);
                    }
                    if (Version == 2)
                    {
                        LstParseXML = ParseXMLInputV2(pXDocument);
                    }
                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            return LstParseXML;
        }


        /// <summary>
        /// Parameter pXML means Xml in a string format,
        /// .It check the particualar method exist or not
        /// If Exist then return true else return false
        /// </summary>
        /// <param name="pXML"></param>
        /// <param name="pEnumMethodName"></param>
        /// <returns></returns>
        public Boolean MethodExist(XDocument pXDocument, MethodNamesProvider.MethodName pEnumMethodName)
        {
            String method = pEnumMethodName.ToString();
            Boolean boolResult = false;
            try
            {
                List<ParseXML> LstParseXML = ParseXMLInput(pXDocument);
                List<ParseXML> Lstresult = (from abc in LstParseXML
                                            where abc.ElementName.Equals(pEnumMethodName.ToString(), StringComparison.OrdinalIgnoreCase) || abc.AttributeName.Equals(pEnumMethodName.ToString(), StringComparison.OrdinalIgnoreCase)
                                            select abc).ToList();

                if (Lstresult != null && Lstresult.Count > 0)
                {
                    boolResult = true;
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }

            return boolResult;
        }
        public Boolean MethodExist(XDocument pXDocument,  List<ParseXML> LstParseXML, MethodNamesProvider.MethodName pEnumMethodName)
        {
            String method = pEnumMethodName.ToString();
            Boolean boolResult = false;
            if (LstParseXML != null && LstParseXML.Count > 0)
            {
                try
                {
                    List<ParseXML> Lstresult = (from abc in LstParseXML
                                                where abc.ElementName.Equals(pEnumMethodName.ToString(), StringComparison.OrdinalIgnoreCase) || abc.AttributeName.Equals(pEnumMethodName.ToString(), StringComparison.OrdinalIgnoreCase)
                                                select abc).ToList();

                    if (Lstresult != null && Lstresult.Count > 0)
                    {
                        boolResult = true;
                    }

                }
                catch (Exception ex)
                {

                    throw ex;
                }
            }
            return boolResult;
        }


        #endregion




    }
}

No comments:

Post a Comment