开发者

remove tags from a xml file written to a string?

开发者 https://www.devze.com 2023-03-30 15:59 出处:网络
this the what the data in the string looks like: <temp><id>TGPU1</id><label>GPU</label><value>67</value></temp><temp><id>THDD1</id>&l

this the what the data in the string 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>

(there is more, that is just a small snipping of the original output.) what i would like to do is feed it through some fast code(not in terms of how long the actual code is, but how long it takes to execute) that will remove all the

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

and output it to a new string with only the value between all the <value>'s and </value>'s. im looking for output in the string like: 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 it only removes the <*pt> which cold be changed to remove something else. but it would only remove one tag at a time. not very fast if i have to remove all tags.

thanks

PS if you wanted to know, the code below is the code i am looking to add this to. this is also the code that printed the original string 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();
}


You can use LINQ:

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

If you're really concerned about performance, you could use XmlReader directly to read through <value> tags and append to a StringBuilder.
However, that's not worth it, unless your XML is hundreds of megabytes large.

0

精彩评论

暂无评论...
验证码 换一张
取 消