public static T GetAttributeValueFromNode <T>(System.Xml.Linq.XElement node, string attributeName, T defaultValue)
        {
            if (node != null && node.Attributes() != null && node.Attributes(attributeName) != null)
            {
                var result = node.Attributes(attributeName).FirstOrDefault();
                if (result != null && string.IsNullOrEmpty(result.Value))
                {
                    return(defaultValue);
                }

                return((T)Convert.ChangeType(result, typeof(T)));
            }
            return(defaultValue);
        }
Beispiel #2
0
        /// <summary>
        /// Creates the XML open tag string for an XElement.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="options">The comment options used to contruct the tag.</param>
        /// <returns>The XML open tag. In case of an element without value, the tag is self-closing.</returns>
        internal static string CreateXmlOpenTag(System.Xml.Linq.XElement element, CodeCommentOptions options)
        {
            var builder = new System.Text.StringBuilder();

            builder.Append("<");
            var name = element.Name.LocalName;

            builder.Append(options.XmlTagsToLowerCase ? name.ToLowerInvariant() : name);

            if (element.HasAttributes)
            {
                foreach (var attr in element.Attributes())
                {
                    builder.Append(CodeCommentHelper.Spacer);
                    builder.Append(attr.ToString());
                }
            }

            if (element.IsEmpty)
            {
                if (options.XmlSpaceSingleTags)
                {
                    builder.Append(CodeCommentHelper.Spacer);
                }

                builder.Append("/");
            }

            builder.Append(">");

            var result = builder.ToString();

            return(options.XmlKeepTagsTogether ? SpaceToFake(result) : result);
        }
        public static T ParseXml(System.Xml.Linq.XElement element)
        {
            var e = (T)Activator.CreateInstance(Type.GetType("Elements." + element.Name));

            Type[] convParamTypes = new Type[] { typeof(string) };
            foreach (var attr in element.Attributes())
            {
                var property = e.GetType().GetProperty(attr.Name.LocalName);
                System.Reflection.MethodInfo conv = property.PropertyType.GetMethod(
                    "op_Explicit", convParamTypes);
                if (conv != null)
                {
                    property.SetValue(e, conv.Invoke(null, new object[] { attr.Value }), null);
                }
                else
                {
                    property.SetValue(e, Convert.ChangeType(attr.Value, property.PropertyType), null);
                }
            }
            foreach (var x in element.Elements())
            {
                e.Elements.Add(ParseXml(x));
            }
            return(e);
        }
Beispiel #4
0
        private static Exception WriteLogEntryToFile(System.Xml.Linq.XElement xmlEntry)
        {
            var format = ConfigurationManager.AppSettings["format"];

            if (xmlEntry == null)
            {
                return(null);
            }

            //If the log file path is not empty but the file is not available it will create it
            if (false == File.Exists(strLogFilePath))
            {
                if (false == CheckDirectory(strLogFilePath))
                {
                    return(null);
                }

                FileStream fs = new FileStream(strLogFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                fs.Close();
            }
            try
            {
                using (var fs = new FileStream(strLogFilePath, FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    using (sw = new StreamWriter(fs))
                    {
                        switch (format)
                        {
                        case "xml":
                            sw.WriteLine(xmlEntry);
                            break;

                        case "json":
                            string json = JsonConvert.SerializeObject(xmlEntry);
                            sw.WriteLine(json);
                            break;

                        case "plain":
                            StringBuilder sb = new StringBuilder();
                            sb.Append(xmlEntry.Name);
                            sb.Append(' ');
                            sb.AppendLine(xmlEntry.Value);
                            foreach (System.Xml.Linq.XAttribute item in xmlEntry.Attributes())
                            {
                                sb.Append(item.Name);
                                sb.Append(' ');
                                sb.AppendLine(item.Value);
                            }
                            sw.WriteLine(sb);
                            break;
                        }
                    }
                }
                return(null);
            }
            catch (Exception ex)
            {
                return(ex);
            }
        }
Beispiel #5
0
        //TODO: add a link for Trinity Configuration File Format Specification v1.
        /// <summary>
        /// Constructs a new configuration entry from an XML element.
        /// </summary>
        /// <param name="entry">The XML element.</param>
        public ConfigurationEntry(System.Xml.Linq.XElement entry)
        {
            Name = entry.Name.LocalName;
            var _settings = new Dictionary <string, ConfigurationSetting>();

            foreach (var attribute in entry.Attributes())
            {
                _settings.Add(attribute.Name.LocalName, new ConfigurationSetting(attribute));
            }
            Settings = _settings;
        }
 public override void ReadAnswerFileNode(System.Xml.Linq.XElement answerFileNode, CustomQuestionProperties props)
 {
     foreach (System.Xml.Linq.XAttribute xmlAttribute in answerFileNode.Attributes())
     {
         if (string.Equals(xmlAttribute.Name.ToString(), _valueGuid.ToString(), StringComparison.OrdinalIgnoreCase))
         {
             string savedValue = xmlAttribute.Value;
             if (savedValue != props.GetAttributeString(_valueGuid))
             {
                 props.UpdateAttribute(_manualChangeFlagGuid, true);
             }
             props.UpdateAttribute(_valueGuid, savedValue);
         }
     }
 }
Beispiel #7
0
        public static bool ElementAttributesEquals(this System.Xml.Linq.XElement a, System.Xml.Linq.XElement b)
        {
            if (a.HasAttributes != b.HasAttributes)
            {
                return(false);
            }

            foreach (System.Xml.Linq.XAttribute at in a.Attributes())
            {
                if (b.Attribute(at.Name) == null || !at.Value.Equals(b.Attribute(at.Name).Value))
                {
                    return(false);
                }
            }

            return(true);
        }
Beispiel #8
0
        private void ReadRecursive(SettingItemGroup settingItem, System.Xml.Linq.XElement element)
        {
            if (element.HasAttributes)
            {
                foreach (var attr in element.Attributes())
                {
                    settingItem.Add(new SettingAttributeItem(attr.Name.LocalName, attr.Value));
                }
            }

            if (element.HasElements)
            {
                foreach (var elm in element.Elements())
                {
                    var item = new SettingItemGroup(elm.Name.LocalName, elm.HasElements ? string.Empty : elm.Value);
                    this.ReadRecursive(item, elm);
                    settingItem.Add(item);
                }
            }
        }
Beispiel #9
0
        //true if all elements of a exists in b, even if b has more
        public static bool ElementMatch(this System.Xml.Linq.XElement a, System.Xml.Linq.XElement b)
        {
            if (!a.Name.LocalName.Equals(b.Name.LocalName))
            {
                return(false);
            }

            if (a.HasElements != b.HasElements)
            {
                return(false);
            }

            if (!a.HasElements && !b.HasElements && !a.Value.Equals(b.Value, StringComparison.CurrentCultureIgnoreCase))
            {
                return(false);
            }

            foreach (System.Xml.Linq.XAttribute at in a.Attributes())
            {
                if (b.Attribute(at.Name) == null || !at.Value.Equals(b.Attribute(at.Name).Value, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(false);
                }
            }

            if (a.HasElements == b.HasElements == true && a.Elements().Count() > b.Elements().Count())
            {
                return(false);
            }

            foreach (var ae in a.Elements())
            {
                var be = b.Elements().Where(s => ae.ElementMatch(s));
                if (!be.Any())
                {
                    return(false);
                }
            }

            return(true);
        }
 public static void MergeXml(System.Xml.Linq.XElement eledest, System.Xml.Linq.XElement elesrc)
 {
     foreach (var attr in elesrc.Attributes())
     {
         eledest.SetAttributeValue(attr.Name, attr.Value);
     }
     foreach (var srcchild in elesrc.Elements())
     {
         //var dstchild = eledest.Element(srcchild.Name);
         //if (dstchild != null)
         //{
         //    MergeXml(dstchild, srcchild);
         //}
         //else
         //{
         //    dstchild = new System.Xml.Linq.XElement(srcchild);
         //    eledest.SetElementValue(srcchild.Name, dstchild);
         //}
         var dstchild = new System.Xml.Linq.XElement(srcchild);
         eledest.Add(dstchild);
     }
 }