[英]remove tags from a xml file written to a string?
這就是字符串中的數據:
<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>
(還有更多,這只是原始輸出的一小部分。)我想做的是通過一些快速代碼(不是實際代碼有多長,但執行需要多長時間)來提供它。這將刪除所有
<temp><id>TGPU1</id><label>GPU</label><value>
並將其輸出到一個新字符串,其中只包含所有<value>
和</value>
的</value>
。 我正在尋找字符串中的輸出,如: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
}
但它只刪除<* pt>哪個冷改變以刪除其他內容。 但它一次只能刪除一個標簽。 如果我必須刪除所有標簽,速度不是很快。
謝謝
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();
}
您可以使用LINQ:
String.Concat(
XElement.Parse(...)
.Descendants("value")
.Select(v => v.Value)
);
如果您真的關心性能,可以直接使用XmlReader
來讀取<value>
標記並附加到StringBuilder
。
但是,這不值得,除非您的XML大到數百兆字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.