chris78
Goto Top

XML Dateien auslesen als Event im loop

Hallo,
ich bin gerade dabei einen Fileinterface zu bauen, was eine XML Datei einließt und dann an einen Webservice übergibt.
Das ganze funktioniert auch... genau ein mal.
Wenn ich eine 2. Datei einlese, bekomme ich eine System.IO.IOException: "The process cannot access the file 'c:\test\test1.xml' because it is being used by another process." Die erste Datei läuft, dann die 2., hier die test1.xml, sagt er, diese wäre im Zugriff.
Mit ist leider nicht bewusst, warum das so ist:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Data;
using Newtonsoft.Json;
using Formatting = System.Xml.Formatting;


//string server = ConfigurationManager.AppSettings.Get("URL"); 
string server = "http://127.0.0.1:1880/test";  
XmlReader oXmlReader = null;
string paths = "";  

string pathsource = ConfigurationManager.AppSettings.Get("Sourcepath");  
string filename = "";  

string destination = ConfigurationManager.AppSettings.Get("Destination");  
string xmlalsstring = "";  


var httpRequest = (HttpWebRequest)WebRequest.Create(server);
httpRequest.Method = "POST";  
httpRequest.ContentType = "application/json";  
httpRequest.Headers.Add("Authorization", "Basic " + "dDUyNzE5NzkyL21lcy1kYXRhOk1FUzIwMjEu");  
httpRequest.Headers.Add("Connection", "keep-alive");  
httpRequest.Headers.Add("Accept-Encoding", "gzip, deflate, br");  

//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain")); 

// Jsion Initial String
var zeit = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss");  
var initialstring = "{\"source\": {\"id\":\"2567463\"},\"text\": \"MACHINE_ROW_DATA\",\"time\": \"" + zeit + "\",\"type\": \"event\", \"DATA\":";  



try

{

    System.IO.FileSystemWatcher watcher = new FileSystemWatcher();

    watcher.Path = pathsource;

    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite

       | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    // Only watch XML files.

    watcher.Filter = "*.xml";  

    //watcher.Changed += new FileSystemEventHandler(OnChanged);

    watcher.Created += new FileSystemEventHandler(OnChanged);

    watcher.EnableRaisingEvents = true;

    void OnChanged(object source, FileSystemEventArgs e)

    {
        paths = e.FullPath;
        filename = e.Name;

        using (StreamReader r = new StreamReader(paths))
        {
            xmlalsstring = r.ReadToEnd();
            r.Close();
        }

     
        XmlDocument doc = new XmlDocument();
       
        doc.LoadXml(xmlalsstring);
        string jsonconvert = JsonConvert.SerializeObject(doc);
        var finalstring = initialstring + jsonconvert + " }";  

        using (var streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
        {
            streamWriter.Write(finalstring);
        }

        var httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            Console.WriteLine("Read to end");  
        }
        Console.WriteLine(httpResponse.StatusCode);
        Console.WriteLine(httpResponse.Server);
        Console.WriteLine(httpResponse.ContentEncoding);
        httpResponse.Close();
        doc = null;

    
        xmlalsstring = null;
        jsonstring = JsonConvert.SerializeObject(finalstring, (global::Newtonsoft.Json.Formatting)Formatting.Indented);
        

        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);  

      


    
         Console.Write("FinalString:"+finalstring);  


       
        


        string path = destination + "\\" + filename;  
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        System.IO.File.Move(paths, path);
        
      
        
       
    }
   
    GC.Collect();
}

catch (Exception ex)

{

    Console.WriteLine(ex.ToString());

}

finally

{
    if (xmlalsstring != null)


    System.Threading.Thread.Sleep(new TimeSpan(0, 0, 1));


}


Console.ReadKey();

Content-Key: 2024791747

Url: https://administrator.de/contentid/2024791747

Printed on: April 23, 2024 at 17:04 o'clock