Exemple #1
0
        private static IPRangesRegion ReadRegionElement(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            var region = new IPRangesRegion();

            while (reader.MoveToNextAttribute())
            {
                var attrName = reader.Name.ToLowerInvariant();
                switch (attrName)
                {
                case "id": region.Id = reader.Value.Trim(); break;

                case "name": region.Name = reader.Value.Trim(); break;

                case "description": region.Description = reader.Value; break;
                }
            }
            reader.MoveToContent();
            return(region);
        }
Exemple #2
0
        private static IPRangesRegion ReadRegionElement(XmlReader reader)
        {
            if (reader == null) throw new ArgumentNullException("reader");

            var region = new IPRangesRegion();
            while (reader.MoveToNextAttribute())
            {
                var attrName = reader.Name.ToLowerInvariant();
                switch (attrName)
                {
                    case "id": region.Id = reader.Value.Trim(); break;
                    case "name": region.Name = reader.Value.Trim(); break;
                    case "description": region.Description = reader.Value; break;
                }
            }
            reader.MoveToContent();
            return region;
        }
Exemple #3
0
        public static bool TryParseFromXml(XmlTextReader reader, out IPRangesGroup group, out Exception exception, bool skipIfRootElementNotMatching)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            exception = null;
            group     = null;

            IPRangesRegion region = null;
            IPRangesGroup  result = null;

            var level = 0;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    level++;
                    try
                    {
                        var name = reader.Name.ToLowerInvariant();
                        if (level == 1)
                        {
                            if (name == "group")
                            {
                                result = ReadGroupElement(reader);
                                continue;
                            }

                            if (skipIfRootElementNotMatching)
                            {
                                group = new IPRangesGroup();
                                return(true);
                            }
                            exception = new XmlException("Invalid root element('" + reader.Name + "'), expecting 'group'");
                            return(false);
                        }

                        if (level == 2 && name == "region")
                        {
                            if (result == null)
                            {
                                exception = new InvalidDataException("Missing appropriate root element");
                                return(false);
                            }
                            region             = ReadRegionElement(reader);
                            region.ParentGroup = result;
                            result.Regions.Add(region);
                            continue;
                        }

                        if (level == 3 && (name == "range" || name == "iprange"))
                        {
                            if (result == null)
                            {
                                exception = new InvalidDataException("Missing 'group' element");
                                return(false);
                            }
                            if (region == null)
                            {
                                exception = new InvalidDataException("Missing 'region' element");
                                return(false);
                            }

                            region.Ranges.Add(ReadRangeElement(reader));
                        }
                    }
                    finally
                    {
                        if (reader.IsEmptyElement)
                        {
                            level--;
                        }
                    }
                    break;

                case XmlNodeType.EndElement:
                    level--;
                    if (level == 0)
                    {
                        group = result;
                        return(true);
                    }
                    break;
                }
            }

            group = result;
            return(true);
        }