0

I'm trying to write xml file starting from JSON data using c#. I see this strange issue: file is removed as soon as created. If I debug the code, I see that file is alive until the using closing statement, after this line, file will be deleted.

Could anyone help me?

Below there is my code:

            string path = "c:\\temp\\";
        // Write JSON to XML file
        string json = "{\"WorkOrderId\":\"WOAA_002_FQjjjjjjjj\",\"WorkOrderName\":\"OP_AAA001\",\"InternalKey\":\"WOAA_002_FQ@iKey@OP_AAA001\",\"parameterDataList\":[{\"Description\":\"Valore PR\",\"InspectionType\":\"Numeric\",\"Value\":\"\",\"LowerLimit\":null,\"NominalValue\":null,\"UpperLimit\":null,\"UoM\":\"n/a\",\"Skill\":true,\"Sequence\":\"1\",\"ParameterValueALTDatetime\":\"\"},{\"Description\":\"Valore PR\",\"InspectionType\":\"String\",\"Value\":\"test\",\"LowerLimit\":null,\"NominalValue\":null,\"UpperLimit\":null,\"UoM\":\"n/a\",\"Skill\":true,\"Sequence\":\"2\",\"ParameterValueALTDatetime\":\"\"}]}";


        XmlDocument uiXmlDoc = JsonConvert.DeserializeXmlNode(json, "root");

        // Get data for compose filename
        string woId = "WOAA_PAOLO";
        string operation = "OP_AAA001";
        string dcId = "TK-18-0000000332";

        if (!path.EndsWith("\\")) path += "\\";

        //Compose filename
        string fileName = path + woId + "@" + operation + "@" + dcId + ".xml";


        //Save the xml and then cleanup
        XmlWriterSettings settings = new XmlWriterSettings { Indent = true };


        using (StreamWriter outStream = new StreamWriter(@fileName))
        {
            XmlWriter writer = XmlWriter.Create(outStream, settings);
            uiXmlDoc.Save(writer);

        }
  • 3
    You must have something else in play here. I see nothing wrong with that code and ran it to double check. No deletion for me. – Broots Waymb Oct 09 '18 at 15:50
  • 1
    XML files raise hackles. Temporarily disable the installed anti-malware product and try again. – Hans Passant Oct 09 '18 at 15:52
  • 2
    I ran your code unmodified and it worked and the file was never deleted. So I think you should check what external process you have that may be running. Like antivirus or malware scanners or if you are in a corporate environment there could be other processes that are deleting the file by design. You can try saving the file to your personal documents folder to see if it works there as well. Also I would recommend you have result of XmlWriter.Create in a using statement as well otherwise the code you have doesn't dispose of that writer. – Rodney S. Foley Oct 09 '18 at 15:53
  • You should be using [`GetTempPath`](https://stackoverflow.com/questions/944483/) and `Path.Combine`, not hard-coding paths and separators. – Dour High Arch Oct 09 '18 at 16:21

2 Answers2

-1

I wouldn't use the root of the c drive, windows security tends to stop users from writing there without admin credentials. Use the environmental variable %temp%, it'll put the file in the user's temp folder. Here's the link for getting the environmental variable in C#

https://learn.microsoft.com/en-us/dotnet/api/system.environment.getenvironmentvariable?view=netframework-4.7.2

The other advantage is you don't have to worry about the folder not being there and it causing an error.

John112358
  • 98
  • 10
  • That's not really true, a normal user can create sub-folders off of C: just fine without elevated rights. I ran his code as a normal user without any issues, and developers do use a local C:\TEMP folder as common practice as its convenient. Corporations may lock down their computers to prevent stuff like this and it has nothing to do with any default Windows Security settings. – Rodney S. Foley Oct 09 '18 at 15:58
  • He did say that when trying a different folder it worked so it had to be permissions. Microsoft has been advising to not write to the root of the C drive for years. Writing temp files in the user temp folder(temp) is the best way to avoid those issues. – John112358 Oct 09 '18 at 19:12
-1

I resolve the problem, the issue is for using the c:/temp directory, if I use another directory it works, I think it could be a security or permission issue.