当前位置: 首页>>代码示例>>C#>>正文


C# XmlReader.ReadEachDescendantAsync方法代码示例

本文整理汇总了C#中System.Xml.XmlReader.ReadEachDescendantAsync方法的典型用法代码示例。如果您正苦于以下问题:C# XmlReader.ReadEachDescendantAsync方法的具体用法?C# XmlReader.ReadEachDescendantAsync怎么用?C# XmlReader.ReadEachDescendantAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Xml.XmlReader的用法示例。


在下文中一共展示了XmlReader.ReadEachDescendantAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadXmlAsync

        /// <summary>
        /// Asynchronously reads data into the current
        /// <see cref="SearchResultStream"/>.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="XmlReader"/> from which to read.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the operation.
        /// </returns>
        public async Task ReadXmlAsync(XmlReader reader)
        {
            var fieldNames = new List<string>();

            this.FieldNames = new ReadOnlyCollection<string>(fieldNames);
            this.IsFinal = true;

            if (!await reader.MoveToDocumentElementAsync("results").ConfigureAwait(false))
            {
                return;
            }

            string preview = reader.GetRequiredAttribute("preview");
            this.IsFinal = !BooleanConverter.Instance.Convert(preview);

            if (!await reader.ReadAsync().ConfigureAwait(false))
            {
                return;
            }

            reader.EnsureMarkup(XmlNodeType.Element, "meta");
            await reader.ReadAsync().ConfigureAwait(false);
            reader.EnsureMarkup(XmlNodeType.Element, "fieldOrder");

            if (!reader.IsEmptyElement)
            {
                await reader.ReadEachDescendantAsync("field", async (r) =>
                {
                    await r.ReadAsync().ConfigureAwait(false);
                    var fieldName = await r.ReadContentAsStringAsync().ConfigureAwait(false);
                    fieldNames.Add(fieldName);
                }).ConfigureAwait(false);

                await reader.ReadEndElementSequenceAsync("fieldOrder", "meta").ConfigureAwait(false);
            }

            if (reader.NodeType == XmlNodeType.Element && reader.Name == "messages")
            {
                //// Skip messages

                await reader.ReadEachDescendantAsync("msg", (r) =>
                {
                    return Task.FromResult(true);
                }).ConfigureAwait(false);

                reader.EnsureMarkup(XmlNodeType.EndElement, "messages");
                await reader.ReadAsync().ConfigureAwait(false);
            }
        }
开发者ID:Netsuye,项目名称:Splunk-SDK,代码行数:59,代码来源:SearchResultMetadata.cs

示例2: ReadXmlAsync

        /// <summary>
        /// Asynchronously reads data into the current <see cref="SearchResult"/>.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="XmlReader"/> from which to read.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the operation.
        /// </returns>
        public async Task ReadXmlAsync(XmlReader reader)
        {
            Contract.Requires<ArgumentNullException>(reader != null, "reader");

            reader.MoveToElement();
            reader.EnsureMarkup(XmlNodeType.Element, "result");

            this.Object = new ExpandoObject();
            var dictionary = (IDictionary<string, object>)this.Object;

            this.SegmentedRaw = null;

            await reader.ReadEachDescendantAsync("field", async (r) =>
            {
                var key = r.GetRequiredAttribute("k");
                var values = new List<string>();
                var fieldDepth = r.Depth;

                while (await r.ReadAsync().ConfigureAwait(false))
                {
                    if (r.Depth == fieldDepth)
                    {
                        break;
                    }

                    Debug.Assert(r.Depth > fieldDepth, "This loop should have exited earlier.");
                    r.EnsureMarkup(XmlNodeType.Element, "value", "v");

                    if (r.Name == "value")
                    {
                        if (await r.ReadToDescendantAsync("text").ConfigureAwait(false))
                        {
                            values.Add(await r.ReadElementContentAsStringAsync().ConfigureAwait(false));
                        }
                    }
                    else if (r.Name == "v")
                    {
                        Debug.Assert(this.SegmentedRaw == null);
                        Debug.Assert(key == "_raw");

                        string value = await r.ReadOuterXmlAsync().ConfigureAwait(false);
                        this.SegmentedRaw = XElement.Parse(value);
                        values.Add(this.SegmentedRaw.Value);
                    }
                }

                switch (values.Count)
                {
                    case 0: 
                        dictionary.Add(key, null);
                        break;
                    case 1:
                        dictionary.Add(key, values[0]);
                        break;
                    default:
                        dictionary.Add(key, new ReadOnlyCollection<string>(values));
                        break;
                }
            }).ConfigureAwait(false);
        }
开发者ID:rob-somerville,项目名称:splunk-sdk-csharp-pcl,代码行数:69,代码来源:SearchResult.cs

示例3: ReadXmlAsync

        /// <summary>
        /// Asynchronously reads data into the current <see cref="SearchResult"/>.
        /// </summary>
        /// <param name="reader">
        /// The <see cref="XmlReader"/> from which to read.
        /// </param>
        /// <returns>
        /// A <see cref="Task"/> representing the operation.
        /// </returns>
        public async Task ReadXmlAsync(XmlReader reader)
        {
            Contract.Requires<ArgumentNullException>(reader != null, "reader");

            reader.MoveToElement();
            reader.EnsureMarkup(XmlNodeType.Element, "result");

            this.Object = new ExpandoObject();
            var dictionary = (IDictionary<string, object>)this.Object;

            this.tagsObject = new ExpandoObject();
            var tagsDictionary = (IDictionary<string, object>)this.tagsObject;

            this.SegmentedRaw = null;

            await reader.ReadEachDescendantAsync("field", async (r) =>
            {
                ImmutableSortedSet<string>.Builder tags = null;

                var key = r.GetRequiredAttribute("k");
                var values = new List<object>();

                var fieldDepth = r.Depth;

                while (await r.ReadAsync().ConfigureAwait(false))
                {
                    if (r.Depth == fieldDepth)
                    {
                        break;
                    }

                    Debug.Assert(r.Depth > fieldDepth, "This loop should have exited earlier.");
                    r.EnsureMarkup(XmlNodeType.Element, "value", "v");

                    if (r.Name == "value")
                    {
                        string value = null;

                        while (await r.ReadAsync().ConfigureAwait(false))
                        {
                            if (r.NodeType == XmlNodeType.EndElement)
                            {
                                r.EnsureMarkup(XmlNodeType.EndElement, "value");
                                break;
                            }

                            r.EnsureMarkup(XmlNodeType.Element, "text", "tag");
                            var isEmptyElement = r.IsEmptyElement;
                            var elementName = r.Name;
                            string content;

                            if (isEmptyElement)
                            {
                                content = string.Empty;
                            }
                            else
                            {
                                await r.ReadAsync().ConfigureAwait(false);
                                content = r.Value;
                            }

                            if (elementName == "tag")
                            {
                                if (tags == null)
                                {
                                    tags = ImmutableSortedSet.CreateBuilder<string>();
                                }
                                tags.Add(content);
                            }
                            else
                            {
                                value = content;
                            }

                            if (!isEmptyElement)
                            {
                                await r.ReadAsync().ConfigureAwait(false);
                                r.EnsureMarkup(XmlNodeType.EndElement, elementName);
                            }
                        }

                        if (tags != null && tags.Count > 0)
                        {
                            values.Add(new TaggedFieldValue(value ?? string.Empty, tags.ToImmutable()));
                            tags.Clear();
                        }
                        else
                        {
                            values.Add(value ?? string.Empty);
                        }
                    }
//.........这里部分代码省略.........
开发者ID:Netsuye,项目名称:Splunk-SDK,代码行数:101,代码来源:SearchResult.cs


注:本文中的System.Xml.XmlReader.ReadEachDescendantAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。