예제 #1
0
        /// <summary>
        /// 通过序列化数据创建<see cref="LocalSegment"/>
        /// </summary>
        /// <param name="element">序列化Xml节点</param>
        /// <returns></returns>
        public static LocalSegment CreateFromXElement(XElement element)
        {
            LocalSegment localSg = new LocalSegment();

            foreach (XElement field in element.Elements())
            {
                switch (field.Name.LocalName)
                {
                case "StartPosition":
                    localSg._startPosition = long.Parse(field.Value);
                    break;

                case "EndPosition":
                    localSg._endPosition = long.Parse(field.Value);
                    break;

                case "Position":
                    localSg._position = long.Parse(field.Value);
                    break;

                default:
                    break;
                }
            }// foreach

            return(localSg);
        }
예제 #2
0
        /// <summary>
        /// 通过配置文件创建<see cref="LocalFileConfig"/>
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <returns></returns>
        public static LocalFileConfig Load(string path)
        {
            XDocument xml;

            // 以UTF-8的格式加载配置文件
            using (var reader = new StreamReader(path, Encoding.UTF8))
            {
                xml = XDocument.Load(reader);
            }

            LocalFileConfig fileInfo = new LocalFileConfig();

            foreach (XElement field in xml.Root.Elements())
            {
                switch (field.Name.LocalName)
                {
                case "Source":
                {
                    if (string.IsNullOrEmpty(field.Value))
                    {
                        break;
                    }

                    fileInfo._mainSource = FileSource.CreateFromXElement(field);
                }
                break;

                case "Mirrors":
                {
                    List <FileSource> mirrors = new List <FileSource>();
                    foreach (var mirror in field.Elements())
                    {
                        mirrors.Add(FileSource.CreateFromXElement(mirror));
                    }

                    fileInfo._mirrors = mirrors;
                }
                break;

                case "RemoteInfo":
                {
                    if (string.IsNullOrEmpty(field.Value))
                    {
                        break;
                    }

                    fileInfo._remoteInfo = RemoteFileInfo.CreateFromXElement(field);
                }
                break;

                case "Segments":
                {
                    List <LocalSegment> segments = new List <LocalSegment>();
                    foreach (var segment in field.Elements())
                    {
                        segments.Add(LocalSegment.CreateFromXElement(segment));
                    }

                    fileInfo._segments = segments;
                }
                break;

                default:
                    break;
                }
            }// foreach

            return(fileInfo);
        }