write and append xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aamersaeed2368
    New Member
    • Dec 2011
    • 13

    write and append xml file

    Hi,

    I have a code which copy nested folders and files from one directory to another and now i want to write xml file for that and when a new folder or file is copied it's entry will be appended in the existing xml file........I have worked on it by using "XmlTextWri ter" class but the problem is that how can i append it and i've also tried "XmlDocumen t" class with that i am unable to even write the xml correctly so anyone here can help me...........I am in real trouble with this.


    Thanks In Advance.
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    #2
    What does your code look like?

    Comment

    • aamersaeed2368
      New Member
      • Dec 2011
      • 13

      #3
      Hi Paul,

      There are two classes in the code one with name "CopyDir" to copy files from source to target directory and i've added another class with name "ClassXml" which write the xml file........Act ually i am sending two objects from "CopyDir" to "ClassXml".

      can i post the whole code here??????????? ??

      Thanks

      Comment

      • Fr33dan
        New Member
        • Oct 2008
        • 57

        #4
        Yes, you can post the whole code here. I think you will need to as I do not understand what information you are writing to the XML file or information is already in the XML file. (Don't forget to use CODE tags)

        Comment

        • aamersaeed2368
          New Member
          • Dec 2011
          • 13

          #5
          The information i want to write is the record of folders and files which are copied from source to destination.The problem is that how can i append the xml for new entry because when the first time program will execute it will create xml file and the next time we need to append it.???????????? ?/

          This is the code for XML class:

          Code:
           class ClassXml
              {
                  
                  public static XmlTextWriter textWriter;
                  public static void create()
                  {
                   
               textWriter = new XmlTextWriter(@"D:\First.xml",Encoding.UTF8);
               textWriter.Formatting = Formatting.Indented;
               textWriter.WriteStartDocument();      
             textWriter.WriteComment("Hold data for copyied folders and files");
               textWriter.WriteStartElement("Folders");
                      
                  }
           public static void writFile(FileInfo fi,DirectoryInfo diSourceSubDir)
                  {
                     
                      textWriter.WriteStartElement("FileName");
                      textWriter.WriteRaw(fi.Name.ToString());
                      textWriter.WriteEndElement();
                      textWriter.WriteStartElement("FileSize");
                      textWriter.WriteRaw(fi.Length.ToString());
                      textWriter.WriteEndElement();
                      textWriter.WriteStartElement("FileCreationTime");
                      textWriter.WriteRaw(fi.CreationTime.ToLongTimeString());
                      textWriter.WriteEndElement();
          
                      
                      
           
                  }
                  public static void writeFolder(DirectoryInfo diSourceSubDir)
                  {
                      textWriter.WriteStartElement("FolderName");
                      textWriter.WriteRaw(diSourceSubDir.Name.ToString());
                      textWriter.WriteStartElement("FolderCreationTime");
            textWriter.WriteRaw(diSourceSubDir.CreationTime.ToLongTimeString());
                       textWriter.WriteEndElement();
                      
                      
                  }
          
              }
          And the code for copy nested folder's and files from source to destination goes here:

          Code:
           
          class CopyDir
              {
                  
           public static void Copy1(string sourceDirectory, string targetDirectory)
                  {
             DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
             DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
                      
                      
                      
                      ClassXml.create();
                      
                      CopyAll(diSource,diTarget);
                  }
          public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
                  {
                     
                      if (Directory.Exists(target.FullName) == false)
                      {
                          Directory.CreateDirectory(target.FullName);
                      }
                      foreach (FileInfo fi in source.GetFiles())
                      {
                      fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
                          
                          ClassXml.writFile(fi,source);
          
                     
                          
                          
                         
                      }
                      foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
                      {
           DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
          
          
          
                          ClassXml.writeFolder(diSourceSubDir);
          
                          
                          
                          
                          CopyAll(diSourceSubDir, nextTargetSubDir);
                      }
                  }
                  public static void GetInfo()
                  {
                      ClassXml.textWriter.WriteEndElement();
                      ClassXml.textWriter.WriteEndElement();
                      ClassXml.textWriter.WriteEndDocument();
                      ClassXml.textWriter.Flush();
                      ClassXml.textWriter.Close();
                      
          
              
                  
                  }
              }

          Comment

          • Fr33dan
            New Member
            • Oct 2008
            • 57

            #6
            aamersaeed2368,
            I got your inbox message, it is not the job of anyone on the forum to fix your code. I (and the other members of the forum) are willing to help you but please acknowledge that we do this in our free time and cannot always devote the time needed to resolve every problem.

            That being said, I have tried various ways of modifying your code to allow for appending but it appears the XmlWriter class is simply not capable of appending data to an existing XML file. This leaves you with 2 options, both of which are going to require a major time commitment/rewrite:
            1.) Write a functionality to read in the old XML data and store it. Then delete the old file and write the data again when you create a new one.
            2.) Re-write your code to write the XML file using the XmlDocument class. Which has functions for loading an existing XML document and adding data to it.(Recommended )

            I know you said that you had trouble using the XmlDocument class but perhaps this tutorial will be of assistance.

            Comment

            • aamersaeed2368
              New Member
              • Dec 2011
              • 13

              #7
              Hi,

              First of all thanks to you that you give time to my problem from your busy schedule....Sec ondly i do acknowledge that nobody has much time to devote here so one more time i am thankful to you.
              Now
              1.The problem using XmlDocument class is that it does not give me that result which i am getting from XmlTextWriter class.Because the copy code copies the nested directories so i want to write the file tags to be nested as well.So in short this nestedness not allow me to use Document class.
              Here is my xml format in which the folders are nested with in each other:

              <?xml version="1.0" encoding="utf-8" ?>
              - <copydata>
              <FileName>Non e</FileName>
              <FileSize>0</FileSize>
              <FileCreationTi me>6:55:28 AM</FileCreationTim e>
              <FileName>Offic e Access 2007</FileName>
              <FileSize>27852 8</FileSize>
              <FileCreationTi me>2:17:32 AM</FileCreationTim e>
              <FileName>Patch </FileName>
              <FileSize>665 6</FileSize>
              <FileCreationTi me>9:12:46 PM</FileCreationTim e>
              <FileName>wor d</FileName>
              <FileSize>994 2</FileSize>
              <FileCreationTi me>1:08:43 AM</FileCreationTim e>
              - <Folder>
              <Name>New folder</Name>
              <FolderCreation Time>1:54:39 AM</FolderCreationT ime>
              <FileName>New Bitmap Image</FileName>
              <FileSize>0</FileSize>
              <FileCreationTi me>1:54:49 AM</FileCreationTim e>
              - <Folder>
              <Name>First</Name>
              <FolderCreation Time>1:54:46 AM</FolderCreationT ime>
              <FileName>fil 1</FileName>
              <FileSize>0</FileSize>
              <FileCreationTi me>1:55:00 AM</FileCreationTim e>
              - <Folder>
              <Name>Demo</Name>
              <FolderCreation Time>1:47:07 AM</FolderCreationT ime>
              - <Folder>
              <Name>Second</Name>
              <FolderCreation Time>6:53:40 AM</FolderCreationT ime>
              <FileName>guzar a</FileName>
              <FileSize>0</FileSize>
              <FileCreationTi me>6:53:52 AM</FileCreationTim e>
              </Folder>
              </Folder>
              </Folder>
              </Folder>
              </copydata>

              Comment

              • Fr33dan
                New Member
                • Oct 2008
                • 57

                #8
                The XmlDocument class does allow for nested nodes. Notice in this example the newTitle XmlElement is added to another XmlElement, newBook. You can do this with your folders. When working with a sub-directory instead of adding the file information to the XmlDocument.Dou cmentElement XmlElement add it to one that represents the folder, then add that one to the root XmlElement.

                Comment

                • aamersaeed2368
                  New Member
                  • Dec 2011
                  • 13

                  #9
                  Hi,

                  Yes you are right that XmlDocument does allow nested tags but you see in my example that closing tags i.e </Folder> are at the end(4 of them). And if i use XmlElement to create new element then it will close like this <Folder>ABC</Folder> i.e right after the text it will close but i want them to be close at the end. Like the tag which is open first will be close at the end.Hope you understand the scenario.

                  Actually from i am a student and i am working on it from 15 days and i am not able to cope with it yet rather i get tention and headache.

                  Comment

                  Working...