附加到大型xml

时间:2009-08-11 09:01:19

标签: c#

我正在编写一个程序来将歌曲的详细信息存储到xml这里是我的代码

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

            mediaXmlDoc.RemoveAll();

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

对于每首歌,我都会附上如下的详细信息

            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);

我的问题是xml的大小增加了追加速度感觉减少我可以做什么来解决这个性能问题

谢谢,

4 个答案:

答案 0 :(得分:7)

你应该真的使用XmlWriter类。 XmlDocument将所有xml加载到内存中,对于大文件,操作可能变得非常慢。

请参阅this example tutorial了解如何使用此课程。

答案 1 :(得分:2)

使用XmlWriter而不是XmlDocument,它更适合大文件

答案 2 :(得分:1)

使用XmlWriter

答案 3 :(得分:1)

   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;
    }