I have a timer that loops through following code every second. this code reads a few xml fragments one after another then prints them to the screen, it then repeats this a second later. outp开发者_运维知识库ut:
34,
23,
12,
What I want to do is to make write all the data to a string like 34,23,12 then returns it so that another class can use it. But if I put it(the string code , you'll see it below) under the if (fragmentReader.Read())
code it doesn't work as it just does the same thing as the console.writeline above it: reads one of the fragments then writes to console, then the next fragment, it doesn't add all the values from the fragment into one string.
thanks
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{
using (var readerz = file.CreateViewAccessor(0, 0))
{
var bytes = new byte[195];
var encoding = Encoding.ASCII;
readerz.ReadArray<byte>(0, bytes, 0, bytes.Length);
//File.WriteAllText("C:\\myFile.txt", encoding.GetString(bytes));
StringReader stringz = new StringReader(encoding.GetString(bytes));
var readerSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
using (var reader = XmlReader.Create(stringz, readerSettings))
{
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value");
//Console.WriteLine(reader.ReadElementContentAsString() + ",");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(reader.ReadElementContentAsString()).Append(",");
Console.WriteLine(sb.ToString());
}
}
}
}
}
}
}
The problem is that in every iteration of the loop you are creating a new stringbuilder object. You need to create that object before your loop, then print the string after the loop.
What you want:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value");
//Console.WriteLine(reader.ReadElementContentAsString() + ",");
sb.Append(reader.ReadElementContentAsString()).Append(",");
}
}
}
Console.WriteLine(sb.ToString());
Try moving the StringBuilder
and the WriteLine
calls outside of the while loop. Since you are making a new StringBuilder
each iteration of the loop you are getting a string with only 1 value (the value you just read).
using (var reader = XmlReader.Create(stringz, readerSettings))
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (reader.Read())
{
using (var fragmentReader = reader.ReadSubtree())
{
if (fragmentReader.Read())
{
reader.ReadToFollowing("value");
sb.Append(reader.ReadElementContentAsString()).Append(",");
}
}
}
Console.WriteLine(sb.ToString());
}
It looks like your StringBuilder
sb is going out of scope. Try moving it so it is being created right after you enter your routine. And to add what @Stephan H stated your WriteLine needs to be placed after you have exited your loop, So that you get the proper output.
static void OnTimedEvent(object source, ElapsedEventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
using (var file = MemoryMappedFile.OpenExisting("AIDA64_SensorValues"))
{ ....
精彩评论