Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / Windows Forms

Transferring Data from a Single XML Stream into Multiple Tables with One Forward-Only Read

Rate me:
Please Sign up or sign in to vote.
4.75/5 (12 votes)
30 Sep 2011CPOL13 min read 42.9K   763   32  
Using parallel implementations of SqlBulkCopy to achieve fast data transfer from a single XML source into multiple tables.
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Text;
using System;

namespace Rob.Utils
{
    public static class XmlUtils
    {
        #region Public Methods
        public static string readAttribute(XElement element, string attribute)
        {
            return readAttribute<string>(element, attribute, val => val);
        }

        public static T readAttribute<T>(XElement element, string attribute, Func<string, T> translator)
        {
            return readAttribute<T>(element, attribute, default(T), translator);
        }

        public static T readAttribute<T>(XElement element, string attribute, T defaultValue, Func<string, T> translator)
        {
            if (element.Attribute(attribute) != null)
                return translator(element.Attribute(attribute).Value);
            else
                return defaultValue;
        }

        public static IEnumerable<XElement> readChildren(XElement element, XName childName)
        {
            foreach (XElement child in element.Elements().Where(child => child.Name.Equals(childName)))
                yield return child;
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) SpiegelSoft
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions