Remove tags from xml file written to string?

this is what the data in the line looks like:

<temp><id>TGPU1</id><label>GPU</label><value>67</value></temp><temp><id>THDD1</id><label>ST3320620AS</label><value>34</value></temp><temp><id>FCPU</id><label>CPU</label><value>1430</value></temp> 

(more, it's just a little discarding the original output). what I would like to do is feed it through some quick code (not how long the actual code works, but how long it takes to execute), which will delete everything

 <temp><id>TGPU1</id><label>GPU</label><value> 

and print it on a new line with only a value between all the <value> and </value> parameters. im looking for output in line: 67341430.
I found this code:

 bool FoundMatch = false; try { Regex regex = new Regex(@"<([be])pt[^>]+>.+?</\1pt>"); while(regex.IsMatch(yourstring) ) { yourstring = regex.Replace(yourstring, ""); } } catch (ArgumentException ex) { // Syntax error in the regular expression } 

but only removes <* pt> that the cold needs to be changed in order to remove something else. but it will only remove one tag at a time. not very fast if I need to remove all tags.

thanks

PS, if you want to know, the code below is the code I want to add. this is also the code that printed the source line mentioned above:

 static void Main(string[] args) { Console.WriteLine("Memory mapped file reader started"); using (var file = MemoryMappedFile.OpenExisting("sensor")) { using (var reader = file.CreateViewAccessor(0, 3800)) { var encoding = Encoding.ASCII; Console.WriteLine(encoding.GetString(bytes)); } } Console.WriteLine("Press any key to exit ..."); Console.ReadLine(); } 
+4
source share
1 answer

You can use LINQ:

 String.Concat( XElement.Parse(...) .Descendants("value") .Select(v => v.Value) ); 

If you are really concerned about performance, you can use the XmlReader directly to read the <value> tags and add to StringBuilder .
However, it is not worth it if your XML does not exceed hundreds of megabytes.

+4
source

Source: https://habr.com/ru/post/1369044/


All Articles