DotNetIsolator icon indicating copy to clipboard operation
DotNetIsolator copied to clipboard

Reading Xml inside Invoke Lamda Expression

Open tamerom opened this issue 2 years ago • 10 comments

I tried to read XML file inside the invoke Lamda Expression but it make exception as below System.Xml.XmlException: Xml_MissingRoot at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlReader.MoveToContent() at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options) at System.Xml.Linq.XElement.Load(String uri, LoadOptions options) at System.Xml.Linq.XElement.Load(String uri)

while i tried to read the xml out side the invoke ,it can be read without any issue , are there any limitation for what i can do inside the invoke Expression?

tamerom avatar Jun 02 '23 12:06 tamerom

Simple question, did you give access to the directory where the file is located?

Guillermo-Santos avatar Jul 04 '23 17:07 Guillermo-Santos

Yes , I did ,

tamerom avatar Jul 04 '23 20:07 tamerom

Well the exception message says that your XML file is missing a root node.

We would need to see your XML file and/or you code reading it to be able to troubleshoot. The exception message alone is not sufficient.

AdamWhiteHat avatar Jul 05 '23 22:07 AdamWhiteHat

using DotNetIsolator; using System.Runtime.InteropServices; using System.Xml.Linq; using Wasmtime;

internal class Program { private static void Main(string[] args) { var wasiConfig = new WasiConfiguration() .WithPreopenedDirectory("PATH THAT CONTAIN XML FILE", "/")

                     .WithInheritedStandardOutput();


    using var host = new IsolatedRuntimeHost()
                        .WithWasiConfiguration(wasiConfig)
                        .WithBinDirectoryAssemblyLoader();
                         
                        
                        

  

    
   using var runtime = new IsolatedRuntime(host);


    runtime.Invoke(() =>
        {
        
             XElement root = XElement.Load(@"/sample.XML");
          
            
            Console.WriteLine($"Hello from {RuntimeInformation.OSArchitecture}");
        });
  
}

}

tamerom avatar Jul 06 '23 15:07 tamerom

you can try with any sample xml you have

tamerom avatar Jul 06 '23 15:07 tamerom

I got the same error, but this worked for me.

using DotNetIsolator;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Linq;
using Wasmtime;

var wasiConfig = new WasiConfiguration()
.WithPreopenedDirectory("PATH THAT CONTAIN XML FILE", "/")
.WithInheritedStandardOutput();

using var host = new IsolatedRuntimeHost()
.WithWasiConfiguration(wasiConfig)
.WithBinDirectoryAssemblyLoader();

using var runtime = new IsolatedRuntime(host);

runtime.Invoke(() =>{
    var root = XElement.Parse(File.ReadAllText(@"/Sample.xml"));
    Console.WriteLine($"Hello from {RuntimeInformation.OSArchitecture}");
    Console.WriteLine($"{root.DescendantNodes().ToArray()[0].ToString()}");
});

This worked too:

using DotNetIsolator;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Linq;
using Wasmtime;

var wasiConfig = new WasiConfiguration()
.WithPreopenedDirectory("PATH THAT CONTAIN XML FILE", "/")
.WithInheritedStandardOutput();

using var host = new IsolatedRuntimeHost()
.WithWasiConfiguration(wasiConfig)
.WithBinDirectoryAssemblyLoader();

using var runtime = new IsolatedRuntime(host);

runtime.Invoke(() =>{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(File.ReadAllText(@"/Sample.xml"));
    XmlNodeList noteNodes = xmlDoc.SelectNodes("/notes/note");
    
    foreach (XmlNode noteNode in noteNodes)
    {
        string to = noteNode.SelectSingleNode("to").InnerText;
        string from = noteNode.SelectSingleNode("from").InnerText;
        string subject = noteNode.SelectSingleNode("subject").InnerText;
        string body = noteNode.SelectSingleNode("body").InnerText;
        Console.WriteLine("To: " + to);
        Console.WriteLine("From: " + from);
        Console.WriteLine("Subject: " + subject);
        Console.WriteLine("Body: " + body);
        Console.WriteLine();
    }
});

Guillermo-Santos avatar Jul 06 '23 17:07 Guillermo-Santos

great thnx, it works when i tested on small files but when i tried with large file about (9 M) size, it gives another error

tamerom avatar Jul 06 '23 18:07 tamerom

her is the error {"error while executing at wasm backtrace:\n 0: 0x40754b - !<wasm function 7705>\n 1: 0x405089 - !<wasm function 7618>\n 2: 0x407a16 - !<wasm function 7714>\n 3: 0xe2e68 - !<wasm function 709>\n 4: 0x2d2c58 - !<wasm function 4879>\n 5: 0x3f7937 - !<wasm function 7321>\n 6: 0x3f77e3 - !<wasm function 7319>\n 7: 0x3f7a2b - !<wasm function 7323>\n 8: 0x3f7ab6 - !<wasm function 7324>\n 9: 0x2b7fce - !<wasm function 4637>\n 10: 0x2b7d14 - !<wasm function 4635>\n 11: 0x30b42c - !<wasm function 5606>\n 12: 0x30ade8 - !<wasm function 5605>\n 13: 0x30ac21 - !<wasm function 5604>\n 14: 0x30ab95 - !<wasm function 5603>\n 15: 0x31ede5 - !<wasm function 5810>\n 16: 0x2ffda1 - !<wasm function 5444>\n 17: 0x2bd5e5 - !<wasm function 4725>\n 18: 0x2292bf - !<wasm function 3284>\n 19: 0x229110 - !<wasm function 3283>\n 20: 0x92199 - !<wasm function 333>\n 21: 0x62d24 - !<wasm function 332>\n 22: 0x367621 - !<wasm function 6309>\n 23: 0x2110a7 - !<wasm function 3085>\n 24: 0x212d90 - !<wasm function 3101>\n 25: 0x21c3f8 - !<wasm function 3187>\n 26: 0xe3202 - !<wasm function 714>\n 27: 0xe3427 - !<wasm function 715>\n 28: 0x4253 - !<wasm function 45>\n 29: 0x419234 - !<wasm function 7923>\n\nCaused by:\n Exited with i32 exit status 1"}

tamerom avatar Jul 06 '23 18:07 tamerom

Do not have a file that large, but maybe a stream can solve this?

runtime.Invoke(() =>{
    using StreamReader reader = new StreamReader(@"/Sample.xml");
    var root = XElement.Load(reader);
    Console.WriteLine($"Hello from {RuntimeInformation.OSArchitecture}");
    Console.WriteLine($"{root.DescendantNodes().ToArray()[0].ToString()}");
});

and

runtime.Invoke(() =>{
    using StreamReader reader = new StreamReader(@"/Sample.xml");
    var xmlDoc = new XmlDocument();
    xmlDoc.Load(reader);
    
    Console.WriteLine("To: " + xmlDoc.ChildNodes.Count);
    XmlNodeList noteNodes = xmlDoc.DocumentElement.SelectNodes("/notes/note");
    foreach (XmlNode noteNode in noteNodes)
    {
        string to = noteNode.SelectSingleNode("to").InnerText;
        string from = noteNode.SelectSingleNode("from").InnerText;
        string subject = noteNode.SelectSingleNode("subject").InnerText;
        string body = noteNode.SelectSingleNode("body").InnerText;
        Console.WriteLine("To: " + to);
        Console.WriteLine("From: " + from);
        Console.WriteLine("Subject: " + subject);
        Console.WriteLine("Body: " + body);
        Console.WriteLine();
    }
});

Note: Using FileStream returns the first error.

Guillermo-Santos avatar Jul 07 '23 16:07 Guillermo-Santos

unfortunately it doesn't solve the issue I have noted that issue 17, it says there is memory crash when it reaches 7.39 MiB https://github.com/SteveSandersonMS/DotNetIsolator/issues/17

tamerom avatar Jul 09 '23 18:07 tamerom