using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace SystemX.Net.Platform.Common.Util
{
    public static class XMLControl
    {
        public static XDocument OpenXMLDocument(string strXmlPath)
        {
            XDocument xDoc = XDocument.Load(strXmlPath);

            return xDoc;
        }

        public static XElement OpenXMLDocument(string strXmlPath, string strRootElemName)
        {
            XDocument xDoc = XDocument.Load(strXmlPath);

            var xElement = xDoc.Element(strRootElemName);

            if (xElement == null) return null;

            return xElement;
        }

        public static XElement OpenXMLElement(XElement xRootElem, string strChildName)
        {
            var xElement = xRootElem.Element(strChildName);

            if (xElement == null) return null;

            return xElement;
        }
        
        public static void LoadXMLAttributes(object objOwner, Type typAttributes, XElement xelem)
        {
            foreach (string strAttrb in Enum.GetNames(typAttributes))
            {
                string strData = xelem.Attribute(strAttrb).Value;

                PropertyInfo propInfo = CommonUtil.GetProperty(objOwner, strAttrb);

                if (propInfo.PropertyType == typeof(int))
                    CommonUtil.SetPropertyValue(objOwner, strAttrb, Convert.ToInt32(strData));
                else if (propInfo.PropertyType == typeof(string))
                    CommonUtil.SetPropertyValue(objOwner, strAttrb, strData);
            }
        }        
    }
}
