Reading Xml inside Invoke Lamda Expression
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?
Simple question, did you give access to the directory where the file is located?
Yes , I did ,
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.
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}");
});
}
}
you can try with any sample xml you have
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();
}
});
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
her is the error
{"error while executing at wasm backtrace:\n 0: 0x40754b -
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.
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