Пример #1
0
        /// <summary>
        /// The read list element from xml.
        /// </summary>
        /// <param name="reader">
        /// The reader.
        /// </param>
        /// <returns>
        /// The <see cref="List"/>.
        /// </returns>
        /// <exception cref="SettingsException">
        /// </exception>
        internal static List <DataCollectorSettings> ReadListElementFromXml(XmlReader reader, string dataCollectorsName)
        {
            List <DataCollectorSettings> settings = new List <DataCollectorSettings>();
            bool empty = reader.IsEmptyElement;

            if (reader.HasAttributes)
            {
                reader.MoveToNextAttribute();
                throw new SettingsException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Resources.Resources.InvalidSettingsXmlAttribute,
                              dataCollectorsName,
                              reader.Name));
            }

            reader.Read();
            if (!empty)
            {
                while (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name.Equals(dataCollectorsName))
                    {
                        settings.Add(DataCollectorSettings.FromXml(reader));
                    }
                    else
                    {
                        throw new SettingsException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.Resources.InvalidSettingsXmlElement,
                                      dataCollectorsName,
                                      reader.Name));
                    }
                }

                reader.ReadEndElement();
            }

            return(settings);
        }
Пример #2
0
        internal static DataCollectorSettings FromXml(XmlReader reader)
        {
            DataCollectorSettings settings = new DataCollectorSettings();

            settings.IsEnabled = true;
            bool empty = reader.IsEmptyElement;

            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    switch (reader.Name)
                    {
                    case "uri":
                        ValidateArg.NotNullOrEmpty(reader.Value, "uri");
                        try
                        {
                            settings.Uri = new Uri(reader.Value);
                        }
                        catch (UriFormatException)
                        {
                            throw new SettingsException(String.Format(CultureInfo.CurrentCulture, Resources.Resources.InvalidDataCollectorUriInSettings, reader.Value));
                        }

                        break;

                    case "assemblyQualifiedName":
                        ValidateArg.NotNullOrEmpty(reader.Value, "assemblyQualifiedName");
                        settings.AssemblyQualifiedName = reader.Value;
                        break;

                    case "friendlyName":
                        ValidateArg.NotNullOrEmpty(reader.Value, "FriendlyName");
                        settings.FriendlyName = reader.Value;
                        break;

                    case "enabled":
                        settings.IsEnabled = bool.Parse(reader.Value);
                        break;

                    case "codebase":
                        settings.CodeBase = reader.Value;     // Optional.
                        break;

                    default:
                        throw new SettingsException(
                                  String.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.Resources.InvalidSettingsXmlAttribute,
                                      Constants.DataCollectionRunSettingsName,
                                      reader.Name));
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(settings.FriendlyName))
            {
                throw new SettingsException(
                          String.Format(CultureInfo.CurrentCulture, Resources.Resources.MissingDataCollectorAttributes, "FriendlyName"));
            }

            reader.Read();
            if (!empty)
            {
                while (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                    case "Configuration":
                        XmlDocument doc     = new XmlDocument();
                        XmlElement  element = doc.CreateElement("Configuration");
                        element.InnerXml       = reader.ReadInnerXml();
                        settings.Configuration = element;
                        break;

                    default:
                        throw new SettingsException(
                                  String.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.Resources.InvalidSettingsXmlElement,
                                      Constants.DataCollectionRunSettingsName,
                                      reader.Name));
                    }
                }
                reader.ReadEndElement();
            }
            return(settings);
        }