Timmi
Timmi

Reputation: 825

Append to large xml

I am writting a program to store the details of songs to xml here is my code

            private XmlDocument mediaXmlDoc = new XmlDocument();
            private XmlNode rootNode;

            mediaXmlDoc.RemoveAll();

            rootNode = mediaXmlDoc.CreateElement("filelist");
            mediaXmlDoc.AppendChild(rootNode);

And for each song i am appending the details like below

            XmlNode file = mediaXmlDoc.CreateElement("file");                

            XmlNode filename = mediaXmlDoc.CreateElement("filename");
            filename.InnerText = FileName;
            file.AppendChild(filename);

            XmlNode path = mediaXmlDoc.CreateElement("path");
            path.InnerText = Path;
            file.AppendChild(path);

            XmlNode ext = mediaXmlDoc.CreateElement("ext");
            ext.InnerText = Ext;
            file.AppendChild(ext);

            XmlNode artist_name = mediaXmlDoc.CreateElement("artist_name");
            artist_name.InnerText = ArtistNameString;
            file.AppendChild(artist_name);

            XmlNode song_title = mediaXmlDoc.CreateElement("song_title");
            song_title.InnerText = SongTitleString;
            file.AppendChild(song_title);

            XmlNode genre = mediaXmlDoc.CreateElement("genre");
            genre.InnerText = GenreString;
            file.AppendChild(genre);

            XmlNode comments = mediaXmlDoc.CreateElement("comments");
            comments.InnerText = CommentsString;
            file.AppendChild(comments);

            XmlNode album_name = mediaXmlDoc.CreateElement("album_name");
            album_name.InnerText = AlbumNameString;
            file.AppendChild(album_name);

            XmlNode year = mediaXmlDoc.CreateElement("year");
            year.InnerText = YearString;
            file.AppendChild(year);

            XmlNode track_number = mediaXmlDoc.CreateElement("track_number");
            track_number.InnerText = TrackNumberString;
            file.AppendChild(track_number);

            XmlNode duration = mediaXmlDoc.CreateElement("duration");
            duration.InnerText = DurationString;
            file.AppendChild(duration);

            XmlNode bit_rate = mediaXmlDoc.CreateElement("bit_rate");
            bit_rate.InnerText = BitRateString;
            file.AppendChild(bit_rate);

            XmlNode protected_media = mediaXmlDoc.CreateElement("protected");
            protected_media.InnerText = ProtectedString;
            file.AppendChild(protected_media);

            XmlNode sample_rate = mediaXmlDoc.CreateElement("sample_rate");
            sample_rate.InnerText = SampleRateString;
            file.AppendChild(sample_rate);

            XmlNode channels = mediaXmlDoc.CreateElement("channels");
            channels.InnerText = ChannelsString;
            file.AppendChild(channels);

            rootNode.AppendChild(file);

and my problem is as the size of the xml increases the append speed feels to decrease what can i do to solve this performance issue

Thanks,

Upvotes: 1

Views: 725

Answers (4)

JAY
JAY

Reputation: 91

   void TraverseNode(string path)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(new StreamReader(path, System.Text.Encoding.UTF8));
        XmlNodeList _nodeList = xmlDocument.SelectNodes("//session");
        string strxml = "";
        string completexml = "";
        for (int inode = 0; inode < _nodeList.Count; inode++)
        {
            if(inode==0)
                strxml = formatxml(_nodeList[inode].OuterXml).ChildNodes[0].ChildNodes[0].OuterXml;
            else
                strxml = formatxml("<session>" + _nodeList[inode].OuterXml + "</session>").ChildNodes[0].ChildNodes[0].OuterXml;

            completexml += strxml;
            strxml = "";

        }
        XmlDocument newxml = new XmlDocument();
        newxml.LoadXml("<session>" + completexml + "</session>");
        newxml.Save(@"C:\Working\Teradata\ssis\out_18.xml");
    }


    XmlDocument formatxml(string xml)
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xml);
        XmlNode nodeListOtherInterest = xmlDocument.SelectSingleNode("//session/data/account/additionalOtherInterest/address");
        XmlNode acAddress = xmlDocument.SelectSingleNode("//session/data/account/address");
        XmlNode locationaddress = xmlDocument.SelectSingleNode("//session/data/account/location/address");
        XmlNode lineaddress = xmlDocument.SelectSingleNode("//session/data/policy/line/address");
        string strotherinterest = "";
        string stracAddress = "";
        string strlocationaddress = "";
        string strlineaddress = "";
        string straddressess="";

        XmlNodeList statecodes;
        statecodes = xmlDocument.SelectNodes("//session/data/policy/line/linestate/linestateterm/coverage/statCode");

        for (int i = 0; i < statecodes.Count; i++)
        {
            if(statecodes[i].InnerText=="0")
                statecodes[i].ParentNode.RemoveChild(statecodes[i]);
        }

        if (xmlDocument.SelectSingleNode("//session/data/account/additionalOtherInterest/address") != null)
        {
            strotherinterest = "<address_otherinterest>" + nodeListOtherInterest.InnerXml + "</address_otherinterest>";
            nodeListOtherInterest.ParentNode.RemoveChild(nodeListOtherInterest);
        }
        if (xmlDocument.SelectSingleNode("//session/data/account/address") != null)
        {
            stracAddress = "<address_Account>" + acAddress.InnerXml + "</address_Account>";
            acAddress.ParentNode.RemoveChild(acAddress);
        }
        if (xmlDocument.SelectSingleNode("//session/data/account/location/address") != null)
        {
            strlocationaddress = "<address_location>" + locationaddress.InnerXml + "</address_location>";
            locationaddress.ParentNode.RemoveChild(locationaddress);
        }
        if (xmlDocument.SelectSingleNode("//session/data/policy/line/address") != null)
        {
            strlineaddress = "<address_line>" + lineaddress.InnerXml + "</address_line>";
            lineaddress.ParentNode.RemoveChild(lineaddress);
        }
        straddressess= "<addressess>"+strotherinterest +stracAddress+strlocationaddress+strlineaddress + "</addressess>";
        XmlDocument address = new XmlDocument();
        address.LoadXml(straddressess);
        XPathNavigator pnav = xmlDocument.CreateNavigator();
        pnav.MoveToChild("session", "");
        pnav.MoveToChild("session","");
        pnav.AppendChild(straddressess);
        return xmlDocument;
    }

Upvotes: 1

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

use XmlWriter

Upvotes: 1

Thomas Levesque
Thomas Levesque

Reputation: 292425

Use XmlWriter rather than XmlDocument, it's better suited for large files

Upvotes: 2

Razzie
Razzie

Reputation: 31222

You should really use the XmlWriter class. XmlDocument loads all the xml in memory, and with huge files, operations can become very slow.

See this example tutorial on how to use this class.

Upvotes: 7

Related Questions