C# XSLT Transforming Large XML Files Quickly

前端 未结 3 1837
我在风中等你
我在风中等你 2021-01-22 09:32

I\'m transforming a > 2GB file with a lookup template in the XSLT. I would like this to run faster but can\'t find any low hanging fruit to improve performance. Any help would b

3条回答
  •  离开以前
    2021-01-22 10:20

    When reading huge xml files always use XmlReader. I like using a combination of XmlReader and Xml linq. I also like using dictionaries. See code below :

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
    
                XmlReader reader = XmlReader.Create(FILENAME);
                while (!reader.EOF)
                {
                    if (reader.Name != "contact")
                    {
                        reader.ReadToFollowing("contact");
                    }
                    if (!reader.EOF)
                    {
                        XElement xContact = (XElement)XElement.ReadFrom(reader);
                        Contact newContact = new Contact();
                        Contact.contacts.Add(newContact);
    
                        newContact.attributes = xContact.Descendants("attribute")
                            .GroupBy(x => (string)x.Element("name"), y => (string)y.Element("value"))
                            .ToDictionary(x => x.Key, y => y.FirstOrDefault());
                    }
                }
            }
        }
        public class Contact
        {
            public static List contacts = new List();
    
            public Dictionary attributes { get; set; }
        }
     }
    

提交回复
热议问题