从写入字符串中的XML文件中删除标签? [英] remove tags from a xml file written to a string?

查看:282
本文介绍了从写入字符串中的XML文件中删除标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个是什么字符串中的数据是这样的:

 <温度>< ID> TGPU1< / ID> ;<标签> GPU< /标签><价值> 67< /值>< /温度><温度>< ID> THDD1< / ID><标签> ST3320620AS< /标签><价值> 34  - ; /值>< /温度><温度>< ID> FCPU< / ID><标签> CPU< /标签><价值> 1430< /值>< /温度> 



(有更多的,即原来的输出的一小剪断。)
我想这样做是给它通过一些快速的代码(不是在实际的代码是如何长期来看,但它需要多长时间来执行),这将删除所有

 <温度>< ID> TGPU1< / ID><标签> GPU< /标签><价值> 

和它输出到一个新的字符串,只有所有的℃之间的值;价值> < /值> 的。即时寻找在字符串输出一样:67341430.结果
I发现这个代码:

 布尔FoundMatch = FALSE ; 

尝试{
正则表达式的regex =新的正则表达式(@≤([是])PT [^>] +方式> +<?/ \1pt>中) ;
,而(regex.IsMatch(yourstring)){
yourstring = regex.Replace(yourstring,);
}
}赶上(ArgumentException的前){正则表达式

//语法错误}

但它仅删除在< * PT>其中冷改为删除别的东西。但它每次只删除一个标记。不是很快,如果我必须删除所有标签。



感谢



PS如果你想知道,下面的代码是我期待它添加到代码。这也是印刷上述原来的字符串代码:

 静态无效的主要(字串[] args)
{
Console.WriteLine(内存映射文件阅读器使用);使用

(var文件= MemoryMappedFile.OpenExisting(传感器))
{
使用(VAR读卡器= file.CreateViewAccessor(0,3800))
{
变种编码= Encoding.ASCII;
Console.WriteLine(encoding.GetString(字节));
}
}

Console.WriteLine(按任意键退出...);
到Console.ReadLine();
}


解决方案

您可以使用LINQ:

  String.Concat(
XElement.Parse(...)
.Descendants(值)
。选择(v => v.Value)
);

如果你的真正的关心性能,你可以使用的XmlReader 直接通过<阅读;价值> 标签,并追加到的StringBuilder 。结果
然而,这不值得的,除非你的XML是百兆大。


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.

这篇关于从写入字符串中的XML文件中删除标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆