Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading Xml inside Invoke Lamda Expression #18

Open
tamerom opened this issue Jun 2, 2023 · 10 comments
Open

Reading Xml inside Invoke Lamda Expression #18

tamerom opened this issue Jun 2, 2023 · 10 comments

Comments

@tamerom
Copy link

tamerom commented Jun 2, 2023

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?

@Guillermo-Santos
Copy link

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

@tamerom
Copy link
Author

tamerom commented Jul 4, 2023

Yes , I did ,

@AdamWhiteHat
Copy link

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.

@tamerom
Copy link
Author

tamerom commented Jul 6, 2023

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
Copy link
Author

tamerom commented Jul 6, 2023

you can try with any sample xml you have

@Guillermo-Santos
Copy link

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();
    }
});

@tamerom
Copy link
Author

tamerom commented Jul 6, 2023

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
Copy link
Author

tamerom commented Jul 6, 2023

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"}

@Guillermo-Santos
Copy link

Guillermo-Santos commented Jul 7, 2023

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.

@tamerom
Copy link
Author

tamerom commented Jul 9, 2023

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
#17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants