2012-04-16 60 views
1

应用的想法很简单,应用程序给出的路径,应用程序写入每个file`s路径转换成XML,我面临的问题是文件名可以有无效字符,并且使应用程序停止工作,这是我用它来解析文件信息转换成XML代码:解析无效字符到XML

// the collecting details method 
    private void Get_Properties(string path) 
    { 
     // Load the XML File 
     XmlDocument xml = new XmlDocument(); 
     xml.Load("Details.xml"); 

     foreach (string eachfile in Files) 
     { 
      try 
      { 
       FileInfo Info = new FileInfo(eachfile); 

       toolStripStatusLabel1.Text = "Adding : " + Info.Name; 

       // Create the Root element 
       XmlElement ROOT = xml.CreateElement("File"); 

       if (checkBox1.Checked) 
       { 
        XmlElement FileName = xml.CreateElement("FileName"); 
        FileName.InnerText = Info.Name; 
        ROOT.AppendChild(FileName); 
       } 

       if (checkBox2.Checked) 
       { 
        XmlElement FilePath = xml.CreateElement("FilePath"); 
        FilePath.InnerText = Info.FullName; 
        ROOT.AppendChild(FilePath); 
       } 

       if (checkBox3.Checked) 
       { 
        XmlElement ModificationDate = xml.CreateElement("ModificationDate"); 
        string lastModification = Info.LastAccessTime.ToString(); 
        ModificationDate.InnerText = lastModification; 
        ROOT.AppendChild(ModificationDate); 
       } 

       if (checkBox4.Checked) 
       { 
        XmlElement CreationDate = xml.CreateElement("CreationDate"); 
        string Creation = Info.CreationTime.ToString(); 
        CreationDate.InnerText = Creation; 
        ROOT.AppendChild(CreationDate); 
       } 

       if (checkBox5.Checked) 
       { 
        XmlElement Size = xml.CreateElement("Size"); 
        Size.InnerText = Info.Length.ToString() + " Bytes"; 
        ROOT.AppendChild(Size); 
       } 

       xml.DocumentElement.InsertAfter(ROOT, xml.DocumentElement.LastChild); 

       // +1 step in progressbar 
       toolStripProgressBar1.PerformStep(); 
       success_counter++; 
       Thread.Sleep(10); 
      } 
      catch (Exception ee) 
      { 
       toolStripProgressBar1.PerformStep(); 

       error_counter++; 
      } 
     } 

     toolStripStatusLabel1.Text = "Now Writing the Details File"; 

     xml.Save("Details.xml"); 

     toolStripStatusLabel1.Text = success_counter + " Items has been added and "+ error_counter +" Items has Failed , Total Files Processed ("+Files.Count+")"; 

     Files.Clear(); 
    } 

下面是XML怎么样子的细节一代又:我想字符

<?xml version="1.0" encoding="utf-8"?> 
<Files> 
    <File> 
    <FileName>binkw32.dll</FileName> 
    <FilePath>D:\ALL DLLS\binkw32.dll</FilePath> 
    <ModificationDate>3/31/2012 5:13:56 AM</ModificationDate> 
    <CreationDate>3/31/2012 5:13:56 AM</CreationDate> 
    <Size>286208 Bytes</Size> 
    </File> 
<File> 

实例解析到XML没有问题:

BX] GC^O^_nI_C {jv_rbp & 1b_H AO & psolher d)做ိiniᖭ

icon_Áq偩侉₳㪏ံぞ鵃_䑋屡1]

MAnaFor줡

EDIT [问题解决]

我不得不做的是: 1转换的文件名UTF8字节 2-转换的UTF8字节回串

这里是方法:

byte[] FilestoBytes = System.Text.Encoding.UTF8.GetBytes(Info.Name); 
string utf8 = System.Text.Encoding.UTF8.GetString(FilestoBytes); 

回答

2

也许XML的格式不正确。 Xml文件不能有一些字符没有被转义。 例如,这是无效的:

<dummy>You & Me</dummy> 

相反,你应该使用:

<dummy>You &amp; Me</dummy> 

在XML非法字符&,<和>(以及“'或属性)

3

目前尚不清楚,你的人物你有问题。只要您使用XML API(而不是试图直接写自己的XML出来),你应该罚款与任何有效的文本(破代理对可能会导致一个问题),但是什么不会是有效的除了制表符,回车符和换行符之外,Unicode代码点的数量少于空格(U + 0020)。他们根本不用于XML。

1

XML中的非法字符是&,<和>(以及属性中的“或”)

在Windows上的文件系统中,只能使用&和'在文件名中(<,>,“不允许使用文件名)

保存XML时,您可以转义这些字符。例如,对于&您将需要&amp;