예제 #1
0
        public static FormValues AsFormValues(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
        {
            var formValues = source as FormValues;

            if (formValues == null)
            {
                formValues = new FormValues();
                foreach (var u in source.GetType().GetProperties(bindingAttr).Where(p => p.GetValue(source, null) != null))
                {
                    formValues.Add(u.Name, u.GetValue(source, null).ToString());
                }
            }
            return(formValues);
        }
예제 #2
0
        public static FormValues FormValues(this XDocument htmlDocument, int formIndex = 1)
        {
            var nodes = htmlDocument.Descendants("form").ElementAt(formIndex - 1).Descendants("input");
            var kv    = new FormValues();

            foreach (var node in nodes)
            {
                var name = node.Attribute("name")?.Value;
                if (name != null)
                {
                    if (kv.ContainsKey(name))
                    {
                        kv[name] = WebUtility.HtmlDecode(node.Attribute("value")?.Value ?? "");
                    }
                    else
                    {
                        kv.Add(name, WebUtility.HtmlDecode(node.Attribute("value")?.Value ?? ""));
                    }
                }
            }
            return(kv);
        }