熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

遍歷現有XML文件,同時寫入到新文件中

2013-11-13 10:21:29  來源: .NET編程 

  用xpathnavigator和實體引用

  xpathnavigator雖然也是以DOM樹方式在內在組織數據但已經過優化可對數據進行修改並且訪問靈活對比XMLLOAD()方式在效率上提升了不少

  在追加時采用實體引用這樣不會把新XML文件全部載入內存用流的方式追加到文件末尾省資源速度快當文件較大時尤為明顯          using SystemXml;
        using SystemXmlXPath;
        using SystemIO;
        class Program
         {
             static StreamWriter sw = new StreamWriter(@secondtxt true EncodingUTF);
             //創建調用的實體 second要寫入的文件
             static XmlTextWriter xtw = new XmlTextWriter(sw);
             static void Main(string[] args)
             {
                 XPathDocument doc = new XPathDocument(@firstxml);
                 //first第一個文件
                 XPathNavigator navigator = docCreateNavigator();
                 navigatorMoveToRoot();
                 xtwFormatting = FormattingIndented;
                 CreatFiles(second); //如果secondxml不存在則在此創建
                 WriteRecursive(navigator); //從根節點開始
                 xtwFlush();
                 xtwClose();
             }
             static private void WriteRecursive(XPathNavigator navigator)
             {
                 if (navigatorHasChildren)
                 {
                     navigatorMoveToFirstChild();
                     WriteNode(navigator);
                     WriteRecursive(navigator);
                     navigatorMoveToParent();
                     while (navigatorMoveToNext())
                     {
                         WriteNode(navigator);
                         WriteRecursive(navigator);
                     }
                     if (navigatorMoveToParent() && navigatorName != root)
                     {
                         xtwWriteEndElement();
                     }
                 }
                 else
                     xtwWriteEndElement();
             }
             static private void WriteNode(XPathNavigator navigator)
             {
                 if (navigatorNodeType == XPathNodeTypeText)
                 {
                     xtwWriteString(navigatorValue);
                     //寫文本節點
                 }
                 else if (navigatorName != stringEmpty)
                 {
                     xtwWriteStartElement(navigatorName);
                     //寫元素節點
                 }
             }
             static private void CreatFiles(string M_str_fname)
             {
                    XmlTextWriter xWriter = new XmlTextWriter(M_str_fname + xml EncodingDefault);
                    xWriterFormatting = FormattingIndented;
                    xWriterWriteStartDocument();
                    xWriterWriteComment(M_str_fname);
                    string name = M_str_fname + txt; //實體文件全名
                    xWriterWriteDocType(Log null null <!ENTITY Log SYSTEM + name + >);//聲明調用文件
                    xWriterWriteStartElement( M_str_fname );//引入調用文件
                    xWriterWriteEntityRef(Log);
                    xWriterWriteEndElement();
                    xWriterWriteEndDocument();
                    xWriterFlush();
                    xWriterClose();
             }
         }

  在vs下調試通過


From:http://tw.wingwit.com/Article/program/net/201311/13362.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.