Exemplo n.º 1
0
 /// <summary>
 /// XMLs to enumerable.
 /// </summary>
 /// <param name="rootName">Name of the root.</param>
 /// <param name="nodeName">Name of the node.</param>
 /// <param name="attributeName">Name of the attribute.</param>
 /// <param name="xml">The XML.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static IEnumerable<string> XmlToEnumerable(string rootName, string nodeName, string attributeName, string xml, XmlToListOptions flag)
 {
     if (string.IsNullOrEmpty(xml))
         yield break;
     bool hasCompositeKey = ((flag & XmlToListOptions.HasCompositeKey) == XmlToListOptions.HasCompositeKey);
     var b = new StringBuilder();
     var z = new XPathDocument(new StringReader(xml));
     var xpathNavigator = z.CreateNavigator();
     var iterator = xpathNavigator.Select("/" + rootName + "/" + nodeName);
     while (iterator.MoveNext())
     {
         if (!hasCompositeKey)
             yield return iterator.Current.GetAttribute(attributeName, string.Empty);
         else
         {
             var pathNavigator = iterator.Current;
             int compositeIndex = 1;
             string compositeAttributeName = attributeName;
             string attributeValue;
             while ((attributeValue = pathNavigator.GetAttribute(compositeAttributeName, string.Empty)) != string.Empty)
             {
                 yield return attributeValue;
                 compositeIndex++;
                 compositeAttributeName = attributeName + compositeIndex.ToString(CultureInfo.InvariantCulture.NumberFormat);
             }
         }
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Lists to XML.
 /// </summary>
 /// <param name="rootName">Name of the root.</param>
 /// <param name="nodeName">Name of the node.</param>
 /// <param name="attributeName">Name of the attribute.</param>
 /// <param name="items">The items.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static string EnumerableToXml(string rootName, string nodeName, string attributeName, IEnumerable<string> items, XmlToListOptions flag)
 {
     if (string.IsNullOrEmpty(rootName))
         throw new ArgumentNullException("rootName");
     if (string.IsNullOrEmpty(nodeName))
         throw new ArgumentNullException("nodeName");
     if (string.IsNullOrEmpty(attributeName))
         throw new ArgumentNullException("attributeName");
     if (items == null)
         throw new ArgumentNullException("list");
     var b = new StringBuilder();
     var w = XmlWriter.Create(b, new XmlWriterSettings() { OmitXmlDeclaration = true });
     w.WriteStartElement(rootName);
     bool hasCompositeKey = ((flag & XmlToListOptions.HasCompositeKey) == XmlToListOptions.HasCompositeKey);
     foreach (string value in items)
     {
         w.WriteStartElement(nodeName);
         if (!hasCompositeKey)
             w.WriteAttributeString(attributeName, value);
         else if (value.Length > 0)
         {
             int compositePropertyIndex = 1;
             string compositePropertyName = attributeName;
             foreach (string compositeValue in value.Split(DefaultListCompositeKeyDivider, StringSplitOptions.None))
             {
                 w.WriteAttributeString(compositePropertyName, compositeValue);
                 compositePropertyIndex++;
                 compositePropertyName = attributeName + compositePropertyIndex.ToString(CultureInfo.InvariantCulture.NumberFormat);
             }
         }
         w.WriteEndElement();
     }
     w.WriteEndElement();
     w.Flush();
     w.Close();
     return b.ToString();
 }
Exemplo n.º 3
0
 /// <summary>
 /// XMLs to enumerable.
 /// </summary>
 /// <param name="xml">The XML.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static IEnumerable<string> XmlToEnumerable(string xml, XmlToListOptions flag) { return XmlToEnumerable("root", "item", "key", xml, flag); }
Exemplo n.º 4
0
 /// <summary>
 /// Lists to XML.
 /// </summary>
 /// <param name="items">The items.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static string EnumerableToXml(IEnumerable<string> items, XmlToListOptions flag) { return EnumerableToXml("root", "item", "key", items, flag); }
Exemplo n.º 5
0
        /// <summary>
        /// XMLs to enumerable.
        /// </summary>
        /// <param name="rootName">Name of the root.</param>
        /// <param name="nodeName">Name of the node.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="xml">The XML.</param>
        /// <param name="flag">The flag.</param>
        /// <returns></returns>
        public static IEnumerable <string> XmlToEnumerable(string rootName, string nodeName, string attributeName, string xml, XmlToListOptions flag)
        {
            if (string.IsNullOrEmpty(xml))
            {
                yield break;
            }
            bool hasCompositeKey = ((flag & XmlToListOptions.HasCompositeKey) == XmlToListOptions.HasCompositeKey);
            var  b = new StringBuilder();
            var  z = new XPathDocument(new StringReader(xml));
            var  xpathNavigator = z.CreateNavigator();
            var  iterator       = xpathNavigator.Select("/" + rootName + "/" + nodeName);

            while (iterator.MoveNext())
            {
                if (!hasCompositeKey)
                {
                    yield return(iterator.Current.GetAttribute(attributeName, string.Empty));
                }
                else
                {
                    var    pathNavigator          = iterator.Current;
                    int    compositeIndex         = 1;
                    string compositeAttributeName = attributeName;
                    string attributeValue;
                    while ((attributeValue = pathNavigator.GetAttribute(compositeAttributeName, string.Empty)) != string.Empty)
                    {
                        yield return(attributeValue);

                        compositeIndex++;
                        compositeAttributeName = attributeName + compositeIndex.ToString(CultureInfo.InvariantCulture.NumberFormat);
                    }
                }
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// XMLs to enumerable.
 /// </summary>
 /// <param name="xml">The XML.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static IEnumerable <string> XmlToEnumerable(string xml, XmlToListOptions flag)
 {
     return(XmlToEnumerable("root", "item", "key", xml, flag));
 }
Exemplo n.º 7
0
        /// <summary>
        /// Lists to XML.
        /// </summary>
        /// <param name="rootName">Name of the root.</param>
        /// <param name="nodeName">Name of the node.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="items">The items.</param>
        /// <param name="flag">The flag.</param>
        /// <returns></returns>
        public static string EnumerableToXml(string rootName, string nodeName, string attributeName, IEnumerable <string> items, XmlToListOptions flag)
        {
            if (string.IsNullOrEmpty(rootName))
            {
                throw new ArgumentNullException("rootName");
            }
            if (string.IsNullOrEmpty(nodeName))
            {
                throw new ArgumentNullException("nodeName");
            }
            if (string.IsNullOrEmpty(attributeName))
            {
                throw new ArgumentNullException("attributeName");
            }
            if (items == null)
            {
                throw new ArgumentNullException("list");
            }
            var b = new StringBuilder();
            var w = XmlWriter.Create(b, new XmlWriterSettings()
            {
                OmitXmlDeclaration = true
            });

            w.WriteStartElement(rootName);
            bool hasCompositeKey = ((flag & XmlToListOptions.HasCompositeKey) == XmlToListOptions.HasCompositeKey);

            foreach (string value in items)
            {
                w.WriteStartElement(nodeName);
                if (!hasCompositeKey)
                {
                    w.WriteAttributeString(attributeName, value);
                }
                else if (value.Length > 0)
                {
                    int    compositePropertyIndex = 1;
                    string compositePropertyName  = attributeName;
                    foreach (string compositeValue in value.Split(_defaultListCompositeKeyDivider, StringSplitOptions.None))
                    {
                        w.WriteAttributeString(compositePropertyName, compositeValue);
                        compositePropertyIndex++;
                        compositePropertyName = attributeName + compositePropertyIndex.ToString(CultureInfo.InvariantCulture.NumberFormat);
                    }
                }
                w.WriteEndElement();
            }
            w.WriteEndElement();
            w.Flush();
            w.Close();
            return(b.ToString());
        }
Exemplo n.º 8
0
 /// <summary>
 /// Lists to XML.
 /// </summary>
 /// <param name="list">The list.</param>
 /// <param name="flag">The flag.</param>
 /// <returns></returns>
 public static string EnumerableToXml(IEnumerable <string> items, XmlToListOptions flag)
 {
     return(EnumerableToXml("root", "item", "key", items, flag));
 }