so this the code snipit:
static void Main(string[] args)
{
Console.WriteLine("Memory mapped file reader started");
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{
using (var reader = file.CreateViewAccessor())
{
var bytes = new byte[3388];
var encoding = Encoding.ASCII;
XmlDocument document = new XmlDocument();
document.LoadXml("<root>" + encoding.GetString(bytes) + "</root>");
XmlNode node = document.DocumentElement.SelectSingleNode("//value");
Console.WriteLine("node = " + node);
}
}
Console.WriteLine("Press any key to exit ...");
Console.ReadLine();
}
it reads info like this from shared memory(snipit):
<sys><id>SCPUCLK</id><label>CPU Clock</label><value>2930</value></sys><sys><id>SCPUMUL</id><label>CPU Multiplier</label><value>11.0</value></sys>
and reads the value in all the <value></value> then prints it.
but its not working quite right, i get invalid character “.” exception when it runs.
so i tried changing “new byte[3388];” the whole length of the string without the <root></root> is 3388(printed to TXT file on hdd to find that out) so i added 13 and got 3401(because thats how long the root tags are, which i had to add to fix multiple root error)
but i still seem to get error about “‘.’, hexadecimal value 0x00, is an invalid character. Line 1, position 7.”
thanks
So a few thoughts:
Your error is caused by non-printing characters in your XML string, which are causing the XML validation in XMLDocument.LoadXML() to fail.
Looking at the MemoryMappedViewAccessor class that is returned (reader), you want to be checking the Capacity property of that, which will be the farthest out you can read. ReadByte() is okay, but if you know your data is ASCII, why not use ReadChar() and append them as you go to a StringBuilder object?
If the data itself has the invalid characters, there’s literally no way to load this correctly during the read process, without first somehow sanitizing these characters from the string. For a scenario like that, I would dump your buffer out to a file, then load that file with a tool like NotePad++ which has a “Show All Characters” function. This will enable you to see specifically what characters and where (if they are outside of the normal XML, this might indicate you still don’t quite have the buffers handling correctly).