2016-07-19 48 views
-2

編輯: Alrighty,終於得到它的工作。這是我的代碼,如果有人需要它C#。幫助表單

public void Save(string savePath, string[] folderPath, string date, string time) 
    { 

     //path for xml 
     //TODO: use "+savePath+" 
     string fileName = date+"_session.xml"; 

     if (File.Exists(fileName) == false) 
     { 
      //ID 
      int sessionId = 1; 


      //creating 
      XDocument doc = new XDocument(
       new XElement("sessions", 
        new XElement("session", 
         new XAttribute("id", sessionId++), 
         new XAttribute("amount", folderPath.Length), 
         new XAttribute("date", date), 
         new XAttribute("time", time)))); 


      XElement folders = doc.Descendants("session").FirstOrDefault(); 
      for (int i = 0; i < folderPath.Length; i++) 
      { 


       folders.Add(new[] { new XElement("folderPath", folderPath[i]) }); 

      } 


      //saving document 
      doc.Save(fileName); 

     } else 
     { 
      XDocument doc = XDocument.Load(fileName); 
      int maxId = doc.Root.Elements("session").Max(t => Int32.Parse(t.Attribute("id").Value)); 
      XElement data = new XElement("session", 
       new XAttribute("id", ++maxId), 
       new XAttribute("amount", folderPath.Length), 
       new XAttribute("date", date), 
       new XAttribute("time", time)); 

      for (int i = 0; i < folderPath.Length; i++) 
      { 
       data.Add(new[] { new XElement("folderPath", folderPath[i]) }); 
      } 
      doc.Root.Add(data); 
      doc.Save(fileName); 


     } 




    } 

OLD CODE

public void Save(string savePath, string[] folderPath, string date, string time 

我創建程序,保存當前打開的文件夾到.txt文件, 我試圖形成一個XML在下面的時尚

<?xml version="1.0" encoding="utf-8"?> 
 
<!--This file is generated by the program.--> 
 
<Folders> 
 
    <Save ID="1"> 
 
    <Amount amount="3"> 
 
    <Date>19.07.2016</Date> 
 
    <Time>22:05</Time> 
 
\t <Folder>C:\\users\test</Folder> 
 
\t <Folder>C:\\program data\\test2</Folder> 
 
\t <Folder>C:\\users\\aleksei\\desktop</Folder> 
 
\t </Amount> 
 
    </Save> 
 
    <Save ID="2"> 
 
    <Amount amount="2"> 
 
    <Date>19.07.2016</Date> 
 
    <Time>23:15</Time> 
 
\t <Folder>C:\\users\test6</Folder> 
 
\t <Folder>C:\\users\\aleksei\\pictures</Folder> 
 
\t </Amount> 
 
    </Save> 
 
</Folders>

我想爲每天生成新的XML,並能夠爲一個保存會話添加多個文件夾。我現在所擁有的

public void Save(string savePath, string folderPath, string date, string time, string amount) 
    { 
    if (File.Exists(date+"_saves.xml") == false) 
     { 
      XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); 
      xmlWriterSettings.Indent = true; 
      xmlWriterSettings.NewLineOnAttributes = true; 
      using (XmlWriter xmlWriter = XmlWriter.Create(date + "_saves.xml", xmlWriterSettings)) 
      { 
       xmlWriter.WriteStartDocument(); 
       xmlWriter.WriteStartElement("Folder"); 

       xmlWriter.WriteStartElement("Amount", amount); 
       xmlWriter.WriteElementString("Date", date); 
       xmlWriter.WriteElementString("Time", time); 
       xmlWriter.WriteElementString("folderPath", folderPath); 
       xmlWriter.WriteEndElement(); 

       xmlWriter.WriteEndElement(); 
       xmlWriter.WriteEndDocument(); 
       xmlWriter.Flush(); 
       xmlWriter.Close(); 
      } 
     } 
     else 
     { 
      XDocument xDocument = XDocument.Load(date + "_saves.xml"); 
      XElement root = xDocument.Element("Folder"); 
      IEnumerable<XElement> rows = root.Descendants("Amount"); 
      XElement firstRow = rows.First(); 
      firstRow.AddBeforeSelf(
       new XElement("Amount", 
       new XElement("Date", date), 
       new XElement("Time", time), 
       new XElement("folderPath", folderPath) 
       )); 
      xDocument.Save(date + "_saves.xml"); 
     } 
    } 
+1

你現在的代碼有什麼問題?什麼不起作用? – ElenaDBA

+0

目前沒有任何工作,這裏給我一個錯誤: XDocument xDocument = XDocument.Load(date +「_saves.xml」); 說: 在System.Xml.dll –

+0

中發生未處理的類型'System.Xml.XmlException'的異常我甚至不知道我在做什麼是正確的。也許使用LINQ好得多? –

回答

0

這是我喜歡創建新的XML文檔

  string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
       "<!--This file is generated by the program.-->" + 
       "<Folders></Folders>"; 
      XDocument doc = XDocument.Parse(header); 
      XElement folders = doc.Descendants("Folders").FirstOrDefault(); 
      folders.Add(new[] { 
       new XElement("Amount", amount), 
       new XElement("Date", date), 
       new XElement("Time", time), 
       new XElement("folderPath", folderPath) 
      }); 
+0

試過了,到目前爲止,它會做一些工作。 –

0

我通常使用下面的方法使用LINQ

創建XML文檔
public static void Save(string savePath, string folderPath, string date, string time, string amount) 
     { 
      string count =" 3"; 
//I am explicitly initializing the id count but you need to query and sort in descedning order take the first id and increment accordingly 
      XDocument xDocument = XDocument.Load(savePath); 
      if (xDocument != null) 
      { 

       //To add the xml document at specific location 
       xDocument.Element("Folders").Elements("Save") 
        .Where(x => x.Attribute("Id").Value == count) 
        .FirstOrDefault() 
        .AddAfterSelf(
        new XElement("Save", new XAttribute("Id", count+1), 
       new XElement("Amount", new XAttribute("amount", "3"), 
       new XElement("Date", date), 
       new XElement("Time", time), 
       new XElement("folder", folderPath))) 
        ); 
       FileStream fs = new FileStream(savePath, 
           FileMode.Create, FileAccess.ReadWrite, FileShare.Read); 

       xDocument.Save(fs); 

      } 

      else 
      { 
       //If folder is open and xml file doesnt exist 
       XDocument newDocument = new XDocument(
       new XDeclaration("1.0", "utf-8", "yes"), 
       new XComment("This file is generated by the program."), 
       //Root single Element    
       new XElement("Folders", 

       //child Elements 
       new XElement("Save", new XAttribute("Id", "1"), 
       new XElement("Amount", new XAttribute("amount", "3"), 
       new XElement("Date", date), 
       new XElement("Time", time), 
       new XElement("folder", folderPath))) 
       )); 

       FileStream fs = new FileStream(savePath, 
           FileMode.Create, FileAccess.ReadWrite, FileShare.Read); 

        newDocument.Save(fs); 

      } 

在這裏,您需要查詢現有的xml文檔中的ID屬性,並按降序排列,如果存在打開的文件夾,則在該ID之後添加新的節點。

+0

我總是收到錯誤,說 其他信息:訪問路徑'C:\ Users \ Public'被拒絕。 或我選擇的任何路徑。 –

+0

以管理員身份運行Visula studio,或給該文件夾提供完全權限 –