示例#1
0
        public void map_attribute_fail_when_attribute_missing()
        {
            var e  = XE.Parse(@"<e a=""123"" />");
            XA  a  = null;
            var ae = Assert.Throws <ArgumentException>(() => MapAttribute(e, "a2", x => a = x));

            Assert.Equal("Expected 'a2' attribute", ae.Message);
        }
示例#2
0
        public void map_attribute_success_when_attribute_present()
        {
            var e = XE.Parse(@"<e a=""123"" />");
            XA  a = null;

            MapAttribute(e, "a", x => a = x);
            Assert.Equal("123", a.Value);
        }
示例#3
0
文件: XML.cs 项目: bl0rq/Waitlist
 public static T GetValue <T> (this System.Xml.Linq.XAttribute xElement, T Default, Func <string, T> fnConverter)
 {
     if (xElement == null)
     {
         return(Default);
     }
     else
     {
         return(fnConverter(xElement.Value));
     }
 }
示例#4
0
文件: XML.cs 项目: bl0rq/Waitlist
 public static string GetValue(this System.Xml.Linq.XAttribute xElement)
 {
     if (xElement == null)
     {
         return("");
     }
     else
     {
         return(xElement.Value);
     }
 }
示例#5
0
 private static string _ReadAttributeString(System.Xml.Linq.XElement xElement, string attributeName)
 {
     if (xElement == null)
     {
         return("");
     }
     System.Xml.Linq.XAttribute xAttribute = xElement.Attribute(attributeName);
     if (xAttribute == null)
     {
         return("");
     }
     return(xAttribute.Value);
 }
示例#6
0
        //Loads a setting based on it's xml representation in the config file
        private void LoadSetting(System.Xml.Linq.XElement setting)
        {
            string name = null, type = null, value = null;

            if (setting.Name.LocalName == "setting")
            {
                System.Xml.Linq.XAttribute xName = setting.Attribute("name");
                if (xName != null)
                {
                    name = xName.Value;
                }

                System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs");
                if (xSerialize != null)
                {
                    type = xSerialize.Value;
                }

                System.Xml.Linq.XElement xValue = setting.Element("value");
                if (xValue != null)
                {
                    if (this[name].GetType() == typeof(System.Collections.Specialized.StringCollection))
                    {
                        foreach (string s in xValue.Element("ArrayOfString").Elements())
                        {
                            if (!((System.Collections.Specialized.StringCollection) this[name]).Contains(s))
                            {
                                ((System.Collections.Specialized.StringCollection) this[name]).Add(s);
                            }
                        }
                    }
                    else
                    {
                        value = xValue.Value;
                    }

                    if (this[name].GetType() == typeof(int))
                    {
                        this[name] = int.Parse(value);
                    }
                    else if (this[name].GetType() == typeof(bool))
                    {
                        this[name] = bool.Parse(value);
                    }
                    else
                    {
                        this[name] = value;
                    }
                }
            }
        }
示例#7
0
        private static void ImportRow(System.Xml.Linq.XElement row
                                      , ImportResult result)
        {
            System.Collections.Generic.List <System.Xml.Linq.XElement> cells = (
                from c in row.Descendants()
                where c.Name == "{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table-cell"
                select c
                ).ToList();

            // DataDto dto = new DataDto();

            int count = cells.Count;
            int j     = -1;

            for (int i = 0; i < count; i++)
            {
                j++;
                System.Xml.Linq.XElement   cell = cells[i];
                System.Xml.Linq.XAttribute attr =
                    cell.Attribute("{urn:oasis:names:tc:opendocument:xmlns:table:1.0}number-columns-repeated");
                if (attr != null)
                {
                    int numToSkip = 0;
                    if (int.TryParse(attr.Value, out numToSkip))
                    {
                        j += numToSkip - 1;
                    }
                }

                if (i > 30)
                {
                    break;
                }
                if (j == 0)
                {
                    // dto.SomeProperty = cells[i].Value;
                }
                if (j == 1)
                {
                    // dto.SomeOtherProperty = cells[i].Value;
                }

                // some more data reading
            } // Next i

            // save data
        } // End Sub ImportRow
示例#8
0
 public static T GetEnumValue <T> (this System.Xml.Linq.XAttribute xElement, T Default)
 {
     try
     {
         if (xElement == null)
         {
             return(Default);
         }
         else
         {
             return((T)Enum.Parse(typeof(T), xElement.GetValue("", s => s)));
         }
     }
     catch (FormatException)
     {
         return(Default);
     }
 }
示例#9
0
            //Loads a setting based on it's xml representation in the config file
            private void LoadSetting(System.Xml.Linq.XElement setting)
            {
                string name = null, type = null, value = null;

                if (setting.Name.LocalName == "setting")
                {
                    System.Xml.Linq.XAttribute xName = setting.Attribute("name");
                    if (xName != null)
                    {
                        name = xName.Value;
                    }

                    System.Xml.Linq.XAttribute xSerialize = setting.Attribute("serializeAs");
                    if (xSerialize != null)
                    {
                        type = xSerialize.Value;
                    }

                    System.Xml.Linq.XElement xValue = setting.Element("value");
                    if (xValue != null)
                    {
                        value = xValue.Value;
                    }
                }


                if (string.IsNullOrEmpty(name) == false &&
                    string.IsNullOrEmpty(type) == false &&
                    string.IsNullOrEmpty(value) == false)
                {
                    switch (name)
                    {
                    //One of the pitfalls is that everytime you add a new
                    //setting to the config file, you will need to add another
                    //case to the switch statement.
                    case "SettingSomething":
                        this[name] = value;
                        break;

                    default:
                        break;
                    }
                }
            }
        /// <summary>
        /// Find the index xml file which contains the Path in the Index section.
        /// </summary>
        public static string GetFileWithPath(string path)
        {
            string file    = "";
            int    mruSize = GetMRUSize();

            for (int i = 0; i < mruSize; i++)
            {
                string xmlFile = GetLastMRUFile(i);
                if (xmlFile != "")
                {
                    try
                    {
                        var stream = new System.IO.FileStream(xmlFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
                        try
                        {
                            System.Xml.Linq.XElement root = System.Xml.Linq.XDocument.Load(stream).Root;
                            if (root != null && root.Name.LocalName == "Index")
                            {
                                // Get index path from XML attribute
                                System.Xml.Linq.XAttribute pathAttribute = root.Attribute("Path");
                                if (pathAttribute != null && pathAttribute.Value != null && pathAttribute.Value == path)
                                {
                                    file = xmlFile;
                                    stream.Close();
                                    break;
                                }
                            }
                        }
                        catch
                        {
                        }
                        stream.Close();
                    }
                    catch
                    {
                    }
                }
            }
            return(file);
        }
示例#11
0
 public void SerializeToAttribute(Point[] objectToSerialize, System.Xml.Linq.XAttribute attrToFill)
 {
     attrToFill.Value = Serialize(objectToSerialize);
 }
示例#12
0
 public static void Validate(this System.Xml.Linq.XAttribute source, System.Xml.Schema.XmlSchemaObject partialValidationType, System.Xml.Schema.XmlSchemaSet schemas, System.Xml.Schema.ValidationEventHandler validationEventHandler, bool addSchemaInfo)
 {
 }
示例#13
0
 public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XAttribute source)
 {
     return(default(System.Xml.Schema.IXmlSchemaInfo));
 }
示例#14
0
 public XAttribute(System.Xml.Linq.XAttribute other)
 {
 }
示例#15
0
 public static T Convert <T>(this Enum type, System.Xml.Linq.XAttribute value) where T : struct
 {
     return(Convert <T>(type, (string)value));
 }
 public static System.Xml.Schema.IXmlSchemaInfo GetSchemaInfo(this System.Xml.Linq.XAttribute source)
 {
     throw null;
 }
示例#17
0
 public void SerializeToAttribute(Point objectToSerialize, System.Xml.Linq.XAttribute attrToFill)
 {
     attrToFill.Value = String.Format("{0}|{1}", objectToSerialize.X.ToString(), objectToSerialize.Y.ToString());
 }
示例#18
0
 public Point DeserializeFromAttribute(System.Xml.Linq.XAttribute attrib)
 {
     return(Deserialize(attrib.Value));
 }
示例#19
0
        /// <summary>
        /// 加载配置文件。
        /// </summary>
        /// <param name="fileName">文件名。</param>
        /// <returns>包含所指定文件内容的配置文件。</returns>
        public static ConfigurationDocument LoadFrom(string fileName)
        {
            // 创建文件对象。
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);
            // 如果文件不存在,则返回空的配置文件对象。
            if (!fileInfo.Exists)
            {
                return(new ConfigurationDocument());
            }
            // 从文件中加载Xml文档对象。
            System.Xml.Linq.XDocument xDocument = System.Xml.Linq.XDocument.Load(fileInfo.FullName);
            // 如果Xml文档的根节点名称不是默认的根节点名称,则返回空的配置文件对象。
            if (xDocument.Root.Name.LocalName != XmlRootName)
            {
                return(new ConfigurationDocument());
            }
            // 创建配置文件对象。
            ConfigurationDocument document = new ConfigurationDocument();

            // 遍历Xml文档根节点下的Xml组节点。
            foreach (System.Xml.Linq.XElement xGroup in xDocument.Root.Elements())
            {
                // 如果Xml组节点的名称是默认的组节点的名称,则继续。
                if (xGroup.Name.LocalName == XmlGroupNodeName)
                {
                    // 获取Xml组节点的key属性。
                    System.Xml.Linq.XAttribute xGroupKeyAttribute = xGroup.Attribute(XmlKeyAttributeName);
                    // 如果key属性不为空且属性值不为空,则继续。
                    if (xGroupKeyAttribute != null)
                    {
                        // 根据xml组节点的key属性值创建并添加配置组对象。
                        ConfigurationGroup configurationGroup = document.GetOrAdd(xGroupKeyAttribute.Value);
                        // 遍历Xml组节点下的Xml项节点。
                        foreach (System.Xml.Linq.XElement xItem in xGroup.Elements())
                        {
                            // 如果Xml项节点的名称是默认的项节点名称,则继续。
                            if (xItem.Name.LocalName == XmlItemNodeName)
                            {
                                // 获取Xml项节点的key属性、value属性、encrypted属性。
                                System.Xml.Linq.XAttribute xItemKeyAttribute       = xItem.Attribute(XmlKeyAttributeName);
                                System.Xml.Linq.XAttribute xItemValueAttribute     = xItem.Attribute(XmlValueAttributeName);
                                System.Xml.Linq.XAttribute xItemEncryptedAttribute = xItem.Attribute(XmlEncryptedAttributeName);
                                // 如果key属性不为空,且key属性值不为空
                                if (xItemKeyAttribute != null)
                                {
                                    // 创建配置项。
                                    ConfigurationItem configurationItem = configurationGroup.GetOrAdd(xItemKeyAttribute.Value);
                                    // 如果value属性不为空,则赋值给配置项。
                                    if (xItemValueAttribute != null)
                                    {
                                        configurationItem.Value = xItemValueAttribute.Value;
                                    }
                                    // 如果encrypted属性不为空,且encrypted属性值不为空,则解密配置项的值。
                                    if (xItemEncryptedAttribute != null && !string.IsNullOrWhiteSpace(xItemEncryptedAttribute.Value))
                                    {
                                        // 如果encrypted属性可以转换成bool类型的值。
                                        if (bool.TryParse(xItemEncryptedAttribute.Value, out bool encrypted))
                                        {
                                            // 赋值配置项的加密属性。
                                            configurationItem.Encrypted = encrypted;
                                            // 根据配置项的加密属性值来解密配置项的值。
                                            if (encrypted)
                                            {
                                                if (Studio.Security.DESManager.TryDecrypt(configurationItem.Value, out string value))
                                                {
                                                    configurationItem.Value = value;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(document);
        }
        private void SavePluginOptions(string application, List<Objects.Option> options, System.Xml.Linq.XElement settingsElement)
        {
            var settingElement = new System.Xml.Linq.XElement("Setting");
            var applicationAttribute = new System.Xml.Linq.XAttribute("Application", application);

            settingElement.Add(applicationAttribute);

            foreach (var option in options)
            {
                var optionElement = new System.Xml.Linq.XElement("Option");
                var optionIdAttr = new System.Xml.Linq.XAttribute("Id", option.OptionId);
                var activeAttr = new System.Xml.Linq.XAttribute("Active", option.Active);

                optionElement.Add(optionIdAttr, activeAttr);

                if (option.Numerics != null && option.Numerics.Count > 0)
                {
                    var numericsElement = new System.Xml.Linq.XElement("Numerics");
                    foreach (int numeric in option.Numerics)
                    {
                        var numericElement = new System.Xml.Linq.XElement("Numeric");
                        numericElement.Add(new System.Xml.Linq.XText(numeric.ToString()));
                        numericsElement.Add(numericElement);
                    }

                    optionElement.Add(numericsElement);
                }

                if (option.Gestures != null && option.Gestures.Count > 0)
                {
                    var gesturesElement = new System.Xml.Linq.XElement("Gestures");
                    foreach (int gesture in option.Gestures)
                    {
                        var gestureElement = new System.Xml.Linq.XElement("Gesture");
                        gestureElement.Add(new System.Xml.Linq.XText(gesture.ToString()));
                        gesturesElement.Add(gestureElement);
                    }

                    optionElement.Add(gesturesElement);
                }

                settingElement.Add(optionElement);
            }

            settingsElement.Add(settingElement);
        }
示例#21
0
 public ConfigurationSetting(System.Xml.Linq.XAttribute attribute)
     : this(attribute.Name.LocalName, attribute.Value)
 {
 }