白开心

  IT博客 :: 首页 ::  :: 联系 :: 聚合  :: 管理 ::
  9 随笔 :: 76 文章 :: 28 评论 :: 0 Trackbacks

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Data;
namespace TestXmlSpace
{
    class Program
    {


        public class Sample
        {
            private static XmlNamespaceManager nsmgr;

            public static void Main()
            {
                Console.WriteLine("Change Start {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                ////UserFileChange(@"e:\Copy (1) of 2263.3 IFRS Package.xml");
                //XmlDocument doc = ChangeWithXML(@"e:\Copy (1) of 2263.3 IFRS Package.xml");
                ////XmlDocument doc = ChangeWithXML(@"e:\HJ.xml");
              
               
                /////修复样式表
                //XmlNodeList Styles = doc.DocumentElement.SelectNodes("Styles/Style[@ss:Name=\"\"]", nsmgr);
                //Console.WriteLine("Repaire Style , Style Counts {0}", Styles.Count );
                //foreach (XmlNode node in Styles)
                //{
                //    node.Attributes["ss:Name"].Value = System.Guid.NewGuid().ToString();
                //}
                //Console.WriteLine("End Repair Style");

 

                //List<string> list = new List<string>();
                //XmlNodeList worksheets = doc.DocumentElement.SelectNodes("ss:Worksheet", nsmgr);

               

                //Console.WriteLine("Worksheets Count {0}", worksheets.Count);
                //foreach (XmlNode sheet in worksheets)
                //{
                //    Console.WriteLine("Worksheet :{0}", sheet.Attributes["ss:Name"].Value);
                //    list.Add(sheet.Attributes["ss:Name"].Value );
                //}
                //SaveWorkSheet(list, doc);
                //Console.WriteLine("Change End {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                Console.WriteLine("压缩文件开始");
                ZipClass.ZipFile("E:\\HJ.xml", "E:\\test1.rar");
                Console.WriteLine("压缩文件结束");
                Console.WriteLine("Change End {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                Console.WriteLine("压缩文件夹开始");
                ZipClass.ZipFileDictory("E:\\ICBC", "E:\\ICBC.rar");
                Console.WriteLine("压缩文件夹结束");
                Console.WriteLine("Change End {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                Console.WriteLine("解压文件夹开始");
                UnZipClass.UnZip("E:\\ICBC.rar", "E:\\UnZip");
                Console.WriteLine("解压文件夹结束");
                Console.WriteLine("Change End {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                Console.WriteLine("解压开始");
                UnZipClass.UnZip( "E:\\test1.rar", "E:\\UnZip");
                Console.WriteLine("解压结束");
                Console.WriteLine("Change End {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                Console.ReadLine();
            }

            private static XmlDocument ChangeWithXML(string xmlPath)
            {
                XmlDocument doc = new XmlDocument();
                doc.XmlResolver = null;
                try
                {
                    doc.Load(xmlPath);
                }
                catch
                {
                    Console.WriteLine("Start To Repaire AS/2 XML In {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    RepairAS2(xmlPath);
                    Console.WriteLine("End Repaire AS/2 XML In {0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    doc.Load(xmlPath);
                }
                nsmgr = new XmlNamespaceManager(doc.NameTable);
                nsmgr.AddNamespace("", "urn:schemas-microsoft-com:office:office");
                nsmgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
                nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
                nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
                nsmgr.AddNamespace("html", "http://www.w3.org/TR/REC-html40");

                GC.Collect();
                GC.Collect(1);

                return doc;
            }

            public static void SaveWorkSheet(IList<string> Sheets,XmlDocument doc)
            {
                XmlNodeList worksheets = doc.DocumentElement.SelectNodes("ss:Worksheet", nsmgr);
                foreach (XmlNode sheet in worksheets)
                {
                    if (!Sheets.Contains(sheet.Attributes["ss:Name"].Value))
                    {
                        doc.DocumentElement.RemoveChild(sheet);
                    }
                }
                doc.Save(@"e:\test1.xml");
            }

            private static void RepairAS2(string xmlPath)
            {
                FileStream fs = new FileStream(xmlPath, FileMode.Open, FileAccess.ReadWrite);
                int location = 1;
                int lens = (int)fs.Length;

                while (location <= lens)
                {
                    int bt = fs.ReadByte();

                    if (bt == 0x0)
                    {
                        fs.Seek(-1, SeekOrigin.Current);
                        fs.WriteByte(0x20);
                    }
                    location++;
                }
                fs.Close();
                fs = null;

                GC.Collect();
                GC.Collect(1);
            }

        }
    }
}

posted on 2007-09-22 17:43 白开心 阅读(264) 评论(0)  编辑 收藏 引用 所属分类: .Net(学习ing...)
只有注册用户登录后才能发表评论。