本文整理汇总了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);
}
}
示例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);
}
示例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);
}
}
//.........这里部分代码省略.........