从内存映射文件读取时使用什么值? C#
所以这是代码 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();
}
它从共享内存(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>
并读取所有
中的值,然后打印它。 但它工作得不太正常,我得到无效字符“。”运行时出现异常。
所以我尝试更改“new byte[3388];”没有
的字符串的整个长度是 3388(打印到硬盘上的 TXT 文件来找出)所以我添加 13 并得到 3401(因为这就是多长)根标签是,我必须添加它来修复多个根错误)
但我似乎仍然收到有关“'.',十六进制值 0x00,是无效字符的错误。行1、位置 7。”
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几点想法:
您的错误是由 XML 字符串中的非打印字符引起的,这导致 XMLDocument.LoadXML() 中的 XML 验证失败。
查看返回的 MemoryMappedViewAccessor 类(读取器),您需要检查它的 Capacity 属性,这将是您可以读取的最远的值。 ReadByte() 没问题,但如果您知道数据是 ASCII,为什么不使用 ReadChar() 并将它们附加到 StringBuilder 对象呢?
如果数据本身具有无效字符,则在读取过程中实际上无法正确加载数据,除非首先以某种方式清理字符串中的这些字符。对于这样的场景,我会将缓冲区转储到一个文件中,然后使用 NotePad++ 等具有“显示所有字符”功能的工具加载该文件。这将使您能够具体查看哪些字符和位置(如果它们位于正常的 XML 之外,这可能表明您仍然没有正确处理缓冲区)。
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).