0

I've recently been trying to add something to an Array in XML using C# .NET 3.5, here is what I have:

    public void WriteToXML(string IP)
    {
        XDocument xmldoc = XDocument.Load("Plugins/SimpleIPBan/SimpleIPBan.configuration.xml");
        XElement parentXElement = xmldoc.XPathSelectElement("BannedIPs");
        XElement newXElement = new XElement("BannedIP", $"{IP}");
        parentXElement.Add(newXElement);
        xmldoc.Save("Plugins/SimpleIPBan/SimpleIPBan.configuration.xml");
    }

I want this code to do the following to the SimpleIPBan.configuration.xml file:

<?xml version="1.0" encoding="utf-8"?>
<ConfigurationSimpleIPBan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <KickOnIPBan>false</KickOnIPBan>
  <KickReason>IP is blacklisted.</KickReason>
  <BannedIPs>
    <BannedIP>00.000.000.000</BannedIP>
    <BannedIP>NewArrayItemHere</BannedIP>
  </BannedIPs>
</ConfigurationSimpleIPBan>

However, when I execute that, I get the following error:

System.InvalidProgramException: Invalid IL code in System.Xml.Linq.XDocument:Load (string): IL_0000: ret


  at SimpleIPBan.SimpleIPBan.WriteToXML (System.String IP) [0x00000] in <filename unknown>:0
  at SimpleIPBan.SimpleIPBan.AddIP (IRocketPlayer Caller, System.String IP) [0x00000] in <filename unknown>:0

I have searched for this error and I saw someone mention the fact that local variables are not defined, however I don't see where I am going wrong. Any help is appreciated.

James Smith
  • 95
  • 1
  • 2
  • 7
  • One of the best methods to manipulate the data of any XML is to serialize the XML first then update the object then de-serialize it. You can use the "Paste Special" in VS to create the XML class(es) automatically. Back to your question: the error mentions something about your file/filename have you verified that the file exists or correctly spelled? Best practise: ALWAYS check for **File.Exists()** before you try to open/load any files. – Casperonian Nov 03 '18 at 00:47
  • @Casperonian But if the data is dynamic (such as IPs being added/removed from the array) will it still allow for them to be read and appended? – James Smith Nov 03 '18 at 00:53
  • When you de-serialize the XML into an object, you can do anything with it just like any other objects. When you're done with the updates, you just Serialize it back into XML file. – Casperonian Nov 03 '18 at 00:55
  • Alright, sorry to be an annoyance but please can you link me somewhere that documents that as I am on my phone currently. Thank you for the quick responses. – James Smith Nov 03 '18 at 01:01
  • Deserializer: https://stackoverflow.com/questions/364253/how-to-deserialize-xml-document – Casperonian Nov 03 '18 at 01:05
  • Serializer: https://stackoverflow.com/questions/11089275/serializing-objects-to-xml-in-c-sharp – Casperonian Nov 03 '18 at 01:06
  • I still appear to be getting the same error: `System.InvalidProgramException: Invalid IL code in System.Xml.Linq.XDocument:Load (string): IL_0000: ret at SimpleIPBan.SimpleIPBan.WriteToXML (System.String filename, System.String IP) [0x00000] in :0 at SimpleIPBan.SimpleIPBan.AddIP (IRocketPlayer Caller, System.String IP) [0x00000] in :0` – James Smith Nov 03 '18 at 11:13
  • If you try to load a different XML document using `XDocument.Load()`, do you still get an error? What about if you `File.ReadAllText()` a string then `XDocument.Parse()` that string? Any different? – spender Nov 03 '18 at 23:37
  • Otherwise, suggest completely uninstalling and reinstalling Mono. – spender Nov 03 '18 at 23:38
  • You should make a test case, unencumbered by the rest of this code. The program should do one thing, `XDocument.Load()`. Try it with this XML above, try it with a different XML. If that fails, then there is indeed something wrong with your installation, and you should re-install or try a different version. – spender Nov 03 '18 at 23:45
  • You write that you are *using C# .NET 3.5* but the string interpolation syntax you use in `$"{IP}"` was added in [c# 6.0](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-6#string-interpolation). Can you please clarify your environment? – dbc Nov 03 '18 at 23:46
  • @spender I've tried it with another plugin, I am getting the same error, however I believe a new one has appeared. Have to use Pastebin as the error is too long. https://pastebin.com/U31SdHRE – James Smith Nov 04 '18 at 00:09
  • Something is expecting this file to exist: `D:\Steam CMD\steamapps\mono\mono\mini\mono.exe` Does it? – spender Nov 04 '18 at 00:24
  • @spender It does not, there is a Mono.dll at `D:\Steam CMD\steamapps\common\Unturned\Unturned_Data\Mono\mono.dll`, but `D:\Steam CMD\steamapps\mono\mono\mini\mono.exe` does not exist. – James Smith Nov 04 '18 at 00:30
  • There's something fishy here... why is mono running out of your steam folders? Anyway, maybe you should try the older `XmlDocument` api instead? I just answered a similar question that should lead the way: https://stackoverflow.com/a/53136618/14357 – spender Nov 04 '18 at 00:36
  • @spender Alright, thanks. I guess it's running from there because the plugins (What I am creating) are running on a Server that uses SteamCMD. I'll have a look at the older api and let you know if it helps. – James Smith Nov 04 '18 at 00:38
  • @spender Seem to be getting the same issue. :/ – James Smith Nov 04 '18 at 01:32

1 Answers1

0

Try following :

    public void WriteToXML(string filename, string IP)
    {
        XDocument xmldoc = XDocument.Load(filename);
        XElement bannedIPs = xmldoc.Descendants("BannedIPs").FirstOrDefault();
        XElement newXElement = new XElement("BannedIP", IP);
        bannedIPs.Add(newXElement);
        xmldoc.Save(filename);
    }    

Here is full working code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace TP3
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument xmldoc = XDocument.Load(FILENAME);
            XElement bannedIPs = xmldoc.Descendants("BannedIPs").FirstOrDefault();

            string IP = "NewArrayItemHere";
            XElement newXElement = new XElement("BannedIP", IP);
            bannedIPs.Add(newXElement);
            xmldoc.Save(FILENAME);
        }
    }

}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Hey, thanks for responding. But I am still getting the same error, no matter what I try: `System.InvalidProgramException: Invalid IL code in System.Xml.Linq.XDocument:Load (string): IL_0000: ret at SimpleIPBan.SimpleIPBan.WriteToXML (System.String filename, System.String IP) [0x00000] in :0 at SimpleIPBan.SimpleIPBan.AddIP (IRocketPlayer Caller, System.String IP) [0x00000] in :0` – James Smith Nov 03 '18 at 10:09
  • You xml file is different from the one you posted. Take posted xml and put into a new file and try again. – jdweng Nov 03 '18 at 12:44
  • Just tried it again, creating a new XML file and put what I originally posted in it and I am still getting the same error. – James Smith Nov 03 '18 at 12:50
  • Open Notepad a paste xml. Then Save AS and make sure the option in box is UTF-8. – jdweng Nov 03 '18 at 12:54
  • The error remains the same. I am using Notepad++, however the `Encode in UTF-8` option is selected, and the file is 100% XML. – James Smith Nov 03 '18 at 12:58
  • I think there is something wrong with the filename. Error says : filename unknown. The filename should have backward slashes not forward slashes. – jdweng Nov 03 '18 at 13:04
  • I've used the direct address to the file, here is when I call the method: `WriteToXML(@"D:\Steam CMD\steamapps\common\Unturned\Servers\Server\Rocket\Plugins\SimpleIPBan\Test.xml", IP);` – James Smith Nov 03 '18 at 13:08
  • Just to let you know, I am creating a plugin for the game Unturned here, and I believe that the `Filename: unknown` issue always happens, no matter what. So I think you can just ignore that and look at the `System.InvalidProgramException: Invalid IL code in System.Xml.Linq.XDocument:Load (string): IL_0000: ret` error. – James Smith Nov 03 '18 at 13:37
  • Thanks for doing this, but I am still getting the same error. Could anything be corrupted? – James Smith Nov 03 '18 at 14:09
  • Do you have all the updates fro Visual Studio and Net Library? What version of Net are you targeting. Di you try my posted code in a new project? – jdweng Nov 03 '18 at 15:45
  • I am using .NET 3.5 and yes, I did try using your code. – James Smith Nov 03 '18 at 16:07