Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Using vb.NET I using XmlDocument to load, modify then save an existing XML file. The loading and modifying work, but I am getting an error "Incorrect function" when trying to save the XML. I am getting this error as the message from a try catch. Even when I comment out my code in the middle of the load and save, I still get this error...

Target framework: .NET Framework 4.5.
Application type: Windows Forms Application

This is a PC issue, as it does work on some Windows PCs but not others. I tried comparing a few things but I am not sure what to look for to compare.

I am new at coding and not sure what to do. Everything that I am Googling is above my head. Can you please help, like you were trying to help a 7 year old.

Thank you in advance!

Code:
VB.NET
Imports System.Xml
Dim ShipXMLDocument As XmlDocument = New XmlDocument()
ShipXMLDocument.Load(FilePath)
Dim objNodeList = Nothing

objNodeList = ShipXMLDocument.DocumentElement.SelectNodes("//ShipShipment/DataArea/Shipment/Header/Parties/Party")

If objNodeList.Count > 0 Then
	For i = 0 To objNodeList.Count - 1
		If objNodeList.Item(i).FirstChild.InnerText <> "SHIPPER" Then
			For ChildNode = 0 To objNodeList.Item(i).ChildNodes.Count - 1
				If objNodeList.Item(i).ChildNodes.Item(ChildNode).Name = "Name" Then
					objNodeList.Item(i).ChildNodes.Item(ChildNode).InnerText = "Tester McTestingstein"
				ElseIf objNodeList.Item(i).ChildNodes.Item(ChildNode).Name = "EmailAddress" Then
					objNodeList.Item(i).ChildNodes.Item(ChildNode).InnerText = "Testing@Testing.com"
				End If
			Next
		End If
	Next
End If

ShipXMLDocument.Save(FilePath)


I am getting this as the stack trace:
Quote:
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 C:\Temp\Send Test Shipment\Send Test Shipment\Functions.vb:line 584


What I have tried:

- Running this code on another Windows PC.
- Commenting out my code in the middle of loading the XML and saving the XML.
Posted
Updated 27-Jan-20 16:39pm
v2
Comments
Christian Graus 3-Jan-20 22:02pm    
If it works on some computers and not others, it means that some library this code uses is not present. Can you check what versions of .NET those computers have installed (although XmlDocument was there from the start I thought)

For i = 0 To objNodeList.Count - 1


VB is pretty crap, but if you need the -1, it's very crap indeed. Are you sure?
JoshuaKo 4-Jan-20 20:51pm    
Christian,

Thank you for your comment. Please note that even when I comment out my code in the middle of loading the XML and saving the XML I still get the same error.

When using (Get-ItemProperty "HKLM:SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Release in PowerShell I get the same answer, "528040". I got this query from https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#ps_a
Christian Graus 4-Jan-20 21:56pm    
I posted a fuller reply as an answer.

Clearly if your saving code blows up, the editing code is irrelevant. Why not try what I suggested instead?
JoshuaKo 4-Jan-20 23:31pm    
I will try to use File.SaveAllText but I wanted try to figure out what the root cause is and not just apply a workaround.

Is there a way to determine which library is being used to confirm if that library is there or corrupted?
Christian Graus 5-Jan-20 0:06am    
According to docs, there's no external library involved, so it all seems odd. TBH I am expecting you to say it also doesn't work and the issue is something else, like you trying to write a protected file.

mports System.Xml
Dim ShipXMLDocument As XmlDocument = New XmlDocument()
ShipXMLDocument.Load(FilePath)
Dim objNodeList = Nothing

objNodeList = ShipXMLDocument.DocumentElement.SelectNodes("//ShipShipment/DataArea/Shipment/Header/Parties/Party")


This makes no sense. Why not just set it's value in the first line?
If objNodeList.Count > 0 Then
	For i = 0 To objNodeList.Count - 1


I doubt that count - 1 is correct. Also, you can do a foreach on the node list instead of having to look them up by index

If objNodeList.Item(i).FirstChild.InnerText <> "SHIPPER" Then
    For ChildNode = 0 To objNodeList.Item(i).ChildNodes.Count - 1
        If objNodeList.Item(i).ChildNodes.Item(ChildNode).Name = "Name" Then


I am fairly certain you could use an xpath to exclude 'SHIPPER' as a value.

objNodeList.Item(i).ChildNodes.Item(ChildNode).InnerText = "Tester McTestingstein"
            ElseIf objNodeList.Item(i).ChildNodes.Item(ChildNode).Name = "EmailAddress" Then

This sort of code turns into spaghetti fast. Use a switch statement to operate no different nodes. Also, if you're looking at node names, not values, you would do better having two node collections at this level, one for each node you want to work with
					objNodeList.Item(i).ChildNodes.Item(ChildNode).InnerText = "Testing@Testing.com"
				End If
			Next
		End If
	Next
End If

ShipXMLDocument.Save(FilePath)


If this is blowing up, I would try storing the XML in a string and using File.SaveAllText to save it and see if that works
 
Share this answer
 
Comments
Richard Deeming 28-Jan-20 14:38pm    
Count - 1 is correct. VB (and VB.NET) is indeed that crap. :)

For...Next Statement - Visual Basic | Microsoft Docs[^]
Update: The issue was the anti Virus.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900