/// <summary> /// Writes multiple values of type <typeparamref name="T"/>, where each value will result in its own /// node being created. Unlike the WriteMany for IEnumerables, this guarentees that ordering will be preserved. /// </summary> /// <typeparam name="T">The Type of value to write.</typeparam> /// <param name="nodeName">Name of the node that will contain the values.</param> /// <param name="values">IEnumerable of values to write. If this value is null, it will be treated /// the same as if it were an empty IEnumerable.</param> /// <param name="writeHandler">Delegate that writes the value to the IValueWriter.</param> public void WriteManyNodes <T>(string nodeName, T[] values, WriteManyNodesHandler <T> writeHandler) { int count; if (values != null) { count = values.Length; } else { count = 0; } WriteStartNode(nodeName); { Write(XmlValueHelper.CountValueKey, count); if (values != null && count > 0) { for (var i = 0; i < values.Length; i++) { var childNodeName = XmlValueHelper.GetItemKey(i); WriteStartNode(childNodeName); writeHandler(this, values[i]); WriteEndNode(childNodeName); } } } WriteEndNode(nodeName); }
/// <summary> /// Writes multiple values of type <typeparamref name="T"/>, where each value will result in its own /// node being created. Ordering is not guarenteed. /// </summary> /// <typeparam name="T">The Type of value to write.</typeparam> /// <param name="nodeName">Name of the node that will contain the values.</param> /// <param name="values">IEnumerable of values to write. If this value is null, it will be treated /// the same as if it were an empty IEnumerable.</param> /// <param name="writeHandler">Delegate that writes the value to the IValueWriter.</param> public void WriteManyNodes <T>(string nodeName, IEnumerable <T> values, WriteManyNodesHandler <T> writeHandler) { int count; if (values != null) { count = values.Count(); } else { count = 0; } WriteStartNode(nodeName); { Write("Count", count); if (values != null && count > 0) { var i = 0; foreach (var value in values) { var childNodeName = XmlValueHelper.GetItemKey(i); WriteStartNode(childNodeName); writeHandler(this, value); WriteEndNode(childNodeName); i++; } } } WriteEndNode(nodeName); }
/// <summary> /// Writes multiple values of the same type to the IValueWriter all under the same node name. /// Ordering is not guarenteed. /// </summary> /// <typeparam name="T">The Type of value to write.</typeparam> /// <param name="nodeName">Name of the node that will contain the values.</param> /// <param name="values">IEnumerable of values to write. If this value is null, it will be treated /// the same as if it were an empty IEnumerable.</param> /// <param name="writeHandler">Delegate that writes the value to the IValueWriter.</param> public void WriteMany <T>(string nodeName, IEnumerable <T> values, WriteManyHandler <T> writeHandler) { int count; if (values != null) { count = values.Count(); } else { count = 0; } WriteStartNode(nodeName); { Write(XmlValueHelper.CountValueKey, count); if (values != null && count > 0) { var i = 0; foreach (var value in values) { writeHandler(XmlValueHelper.GetItemKey(i), value); i++; } } } WriteEndNode(nodeName); }
/// <summary> /// Reads multiple values that were written with WriteMany. /// </summary> /// <typeparam name="T">The Type of value to read.</typeparam> /// <param name="nodeName">The name of the node containing the values.</param> /// <param name="readHandler">Delegate that reads the values from the IValueReader.</param> /// <returns>Array of the values read the IValueReader.</returns> public T[] ReadMany <T>(string nodeName, ReadManyHandler <T> readHandler) { var nodeReader = ReadNode(nodeName); var count = nodeReader.ReadInt(XmlValueHelper.CountValueKey); var ret = new T[count]; for (var i = 0; i < count; i++) { var key = XmlValueHelper.GetItemKey(i); ret[i] = readHandler(nodeReader, key); } return(ret); }
/// <summary> /// Reads multiple nodes that were written with WriteMany. /// </summary> /// <typeparam name="T">The Type of nodes to read.</typeparam> /// <param name="nodeName">The name of the root node containing the values.</param> /// <param name="readHandler">Delegate that reads the values from the IValueReader.</param> /// <param name="handleMissingKey">Allows for handling when a key is missing or invalid instead of throwing /// an <see cref="Exception"/>. This allows nodes to be read even if one or more of the expected /// items are missing. The returned array will contain null for these indicies. The int contained in the /// <see cref="Action{T}"/> contains the 0-based index of the index that failed. This parameter is only /// valid when <see cref="IValueReader.SupportsNameLookup"/> and <see cref="IValueReader.SupportsNodes"/> are true. /// Default is null.</param> /// <returns> /// Array of the values read from the IValueReader. /// </returns> public T[] ReadManyNodes <T>(string nodeName, ReadManyNodesHandler <T> readHandler, Action <int, Exception> handleMissingKey) { if (handleMissingKey == null) { return(ReadManyNodes(nodeName, readHandler)); } var nodeReader = ReadNode(nodeName); var count = nodeReader.ReadInt(XmlValueHelper.CountValueKey); var ret = new T[count]; for (var i = 0; i < count; i++) { var key = XmlValueHelper.GetItemKey(i); try { var childNodeReader = nodeReader.ReadNode(key); ret[i] = readHandler(childNodeReader); } catch (Exception ex) { const string errmsg = "Failed to read key `{0}` (index: {1}) from `{2}` when using ReadManyNodes on nodeName `{3}`. handleMissingKey argument was specified, so loading will resume..."; if (log.IsWarnEnabled) { log.WarnFormat(errmsg, key, i, this, nodeName); } handleMissingKey(i, ex); ret[i] = default(T); } } return(ret); }