我怎样才能在这些标签之间删除 <value></value>
How can i delete between these tags <value></value>
我问过这个问题,但没答对。我想删除值标签之间的输出,但我正在这样做。我只想删除数据中的值标签,即我的 table。你能帮我删除这些值吗?请记住,这些输出不是硬编码的,用户只是输入了这些值,所以每次我加载表单时,我都想删除标签之间的那些输出。
<data name="CurrentSelectedUser_Label" xml:space="preserve">
<value>Current selected record:</value>
<comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
</data>
<data name="FinEnrolment_Exit_Button" xml:space="preserve">
<value>Exit Finger Enrolment</value>
<comment>[Font]Regular[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp] [Comment] this is a comment.!![/Comment]</comment>
</data>
我试过了,但是我没有指定数据中的值标签,这是我的 table。请参阅下面的代码;
string text = File.ReadAllText(outputFilePath);
text = DeleteBetween1(text, "<value>", "</value>");
public string DeleteBetween1(string STR, string FirstString, string LastString)
{
string regularExpressionPattern1 = "<value(.*?)</value>";
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
MatchCollection collection = regex.Matches(STR.ToString());
var val = string.Empty;
foreach (Match m in collection)
{
val = m.Groups[1].Value;
STR = STR.Replace(val, "");
}
STR = STR.Replace(val, "");
return STR;
}
对之前的回答表示歉意,我已经在 linqpad 中编写了一个测试程序,它可以实现您想要的。
希望这种方法对你有用。
我必须插入一个根节点,否则 XML 将无法解析,我将其称为根节点。
void Main()
{
string xml = @"
<root>
<data name=""CurrentSelectedUser_Label"" xml:space=""preserve"">
<value>Current selected record:</value>
<comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
</data>
<data name=""FinEnrolment_Exit_Button"" xml:space=""preserve"">
<value>Exit Finger Enrolment</value>
<comment>[Font]Regular[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp] [Comment] this is a comment.!![/Comment]</comment>
</data></root>";
var doc = XDocument.Load(new StringReader(xml));
var nodes = doc.XPathSelectElements("/root/data/value");
foreach (var node in nodes)
{
((XElement)node).Value = string.Empty;
}
... output the doc here.
}
使用 Linq 到 XML
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("XMLFile1.xml");
foreach(var valueNode in doc.Descendants("data").SelectMany(n=> n.Elements("value")))
{
valueNode.Value = string.Empty;
}
}
}
我问过这个问题,但没答对。我想删除值标签之间的输出,但我正在这样做。我只想删除数据中的值标签,即我的 table。你能帮我删除这些值吗?请记住,这些输出不是硬编码的,用户只是输入了这些值,所以每次我加载表单时,我都想删除标签之间的那些输出。
<data name="CurrentSelectedUser_Label" xml:space="preserve">
<value>Current selected record:</value>
<comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
</data>
<data name="FinEnrolment_Exit_Button" xml:space="preserve">
<value>Exit Finger Enrolment</value>
<comment>[Font]Regular[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp] [Comment] this is a comment.!![/Comment]</comment>
</data>
我试过了,但是我没有指定数据中的值标签,这是我的 table。请参阅下面的代码;
string text = File.ReadAllText(outputFilePath);
text = DeleteBetween1(text, "<value>", "</value>");
public string DeleteBetween1(string STR, string FirstString, string LastString)
{
string regularExpressionPattern1 = "<value(.*?)</value>";
Regex regex = new Regex(regularExpressionPattern1, RegexOptions.Singleline);
MatchCollection collection = regex.Matches(STR.ToString());
var val = string.Empty;
foreach (Match m in collection)
{
val = m.Groups[1].Value;
STR = STR.Replace(val, "");
}
STR = STR.Replace(val, "");
return STR;
}
对之前的回答表示歉意,我已经在 linqpad 中编写了一个测试程序,它可以实现您想要的。 希望这种方法对你有用。
我必须插入一个根节点,否则 XML 将无法解析,我将其称为根节点。
void Main()
{
string xml = @"
<root>
<data name=""CurrentSelectedUser_Label"" xml:space=""preserve"">
<value>Current selected record:</value>
<comment>[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment] this is a comment.!![/Comment]</comment>
</data>
<data name=""FinEnrolment_Exit_Button"" xml:space=""preserve"">
<value>Exit Finger Enrolment</value>
<comment>[Font]Regular[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp] [Comment] this is a comment.!![/Comment]</comment>
</data></root>";
var doc = XDocument.Load(new StringReader(xml));
var nodes = doc.XPathSelectElements("/root/data/value");
foreach (var node in nodes)
{
((XElement)node).Value = string.Empty;
}
... output the doc here.
}
使用 Linq 到 XML
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load("XMLFile1.xml");
foreach(var valueNode in doc.Descendants("data").SelectMany(n=> n.Elements("value")))
{
valueNode.Value = string.Empty;
}
}
}