从写入字符串的xml文件中删除标签?

时间:2022-08-26 22:35:24

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:

并将其输出到一个新字符串,其中只包含所有 和 之间的值。我正在寻找字符串中的输出,如:67341430。我发现此代码:

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.

但它只删除<* pt>哪个冷改变以删除别的东西。但它一次只能删除一个标签。如果我必须删除所有标签,速度不是很快。

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:

PS如果你想知道,下面的代码是我想要添加的代码。这也是打印上面提到的原始字符串的代码:

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();
}

1 个解决方案

#1


4  

You can use LINQ:

您可以使用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.

如果您真的关心性能,可以直接使用XmlReader来读取 标记并附加到StringBuilder。但是,这不值得,除非您的XML大到数百兆字节。

#1


4  

You can use LINQ:

您可以使用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.

如果您真的关心性能,可以直接使用XmlReader来读取 标记并附加到StringBuilder。但是,这不值得,除非您的XML大到数百兆字节。