none
XmlDocument isn't Releasing My XML File for Saving RRS feed

  • Question

  • I would like to load an XML from a file, modify it, then save the XML to itself. However, I am getting an error when I try to write to the same file. I can create a new file, but that is not my goal. Since there is no close/dispose option for XmlDocument, I don't know how to force it. I did try GC.Collect() even though it is not recommend, but it didn't help either-way.


    Dim ShipXMLDocument As XmlDocument = New XmlDocument()
    ShipXMLDocument.Load(FilePath)
    
    'Modify XML....
    
    ShipXMLDocument.Save(FilePath)

    I am new at coding so please explain the solution like I am a 7 year old.

    Thank you!

    Thank you!

    Sunday, January 5, 2020 7:01 AM

Answers

All replies

  • Hi JKKDevOPS,

    1. What exactly is the error?
    2. It is better to use LINQ to XML API in the .Net Framework while dealing with XML.

    Check it out. The code below is using XDocument type to handle XML document.

    void Main()
    {
    	const string fileName = @"e:\Temp\ShipXMLDocument.xml";
    	
    	XDocument ShipXMLDocument = XDocument.Load(fileName);
    	ShipXMLDocument.Root.Add(new XElement("city", "Miami"));
    	ShipXMLDocument.Save(fileName);
    }
    Sunday, January 5, 2020 3:33 PM
  • Yitzhak,

    1. 

    Error Message: Incorrect function.
    Error StackTrace: 
       at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
       at Apollo_Shipping.Functions.XML_ScrubContactInformation(Object FilePath) in \\xxxxxxxxx\Functions.vb:line 610

    2. 

    I tried using your suggestion but got the same error message.

    Error Message: Incorrect function.

    Error StackTrace: 
       at Microsoft.VisualBasic.CompilerServices.Symbols.Container.InvokeMethod(Method TargetProcedure, Object[] Arguments, Boolean[] CopyBack, BindingFlags Flags)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.CallMethod(Container BaseReference, String MethodName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, BindingFlags InvocationFlags, Boolean ReportErrors, ResolutionFailure& Failure)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
       at Apollo_Shipping.Functions.XML_ScrubContactInformation(Object FilePath) in \\xxxxxxxxx\Functions.vb:line 597

    Dim ShipXMLDocument As XDocument = XDocument.Load(FilePath)
    'Dim objNodeList = ShipXMLDocument.Descendants("Party")
    Try
    If PublicVariables.RunDebug Then Call Functions.Debug_Run("Log", "20190611_2.10.1", System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString, "In", "Going to call to save XML after GC 8", Nothing, Nothing)
    ShipXMLDocument.Save(FilePath)
                        If PublicVariables.RunDebug Then Call Functions.Debug_Run("Log", "20190611_2.10.2", System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString, "In", "Saved XML", Nothing, Nothing)
                    Catch ex As Exception
                        Dim ErrorNumber As String = "20190611_2.100"
                        If PublicVariables.RunDebug Then
                            Call Functions.Debug_Run("ERROR", ErrorNumber, System.Reflection.MethodInfo.GetCurrentMethod().Name.ToString, "In", "XML Saving Error", ex.Message, ex.StackTrace)
                        Else
                            Call Functions.Error_ShowMessage(ErrorNumber, ex.Message, ex.StackTrace, Nothing)
                        End If
                    End Try

    Sunday, January 5, 2020 7:27 PM
  • Hi JKKDevOPS,

    1. I don't understand your error. Your code has lots of additional functionality.
      Unfortunately, the 'vb:line 597' doesn't tell us what line of code is failing.
    2. Is it possible that your file system permissions are configured for read-only access?

    Here is the same working code in VB.NET:

    Sub Main
    	Const fileName As String = "e:\Temp\ShipXMLDocument.xml"
    
    	Dim ShipXMLDocument As XDocument = XDocument.Load(fileName)
    	ShipXMLDocument.Root.Add(New XElement("city", "Miami"))
    	ShipXMLDocument.Save(fileName)
    End Sub
    Sunday, January 5, 2020 7:52 PM
  • Update: The issue was the anti Virus.
    • Marked as answer by JKKDevOPS Tuesday, January 28, 2020 3:40 AM
    Tuesday, January 28, 2020 3:40 AM