C# 如何在这些标记之间删除<;价值></价值>;
我问了这个问题,但没有答对。我想删除值标记之间的输出,但我正打算这样做。我只想删除作为我的表的数据中的值标记。你能帮我删除那些值吗。请记住,这些输出不是硬编码的,用户只是输入了这些值,因此每次加载表单时,我都希望删除标记之间的输出C# 如何在这些标记之间删除<;价值></价值>;,c#,tags,C#,Tags,我问了这个问题,但没有答对。我想删除值标记之间的输出,但我正打算这样做。我只想删除作为我的表的数据中的值标记。你能帮我删除那些值吗。请记住,这些输出不是硬编码的,用户只是输入了这些值,因此每次加载表单时,我都希望删除标记之间的输出 <data name="CurrentSelectedUser_Label" xml:space="preserve"> <value>Current selected record:</value> <co
<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>
当前选定记录:
[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment]这是一条评论。!![/评论]
退出手指注册
[Font]常规[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp][Comment]这是一条评论。!![/评论]
我尝试了这一点,但未能指定作为表的数据中的值标记。见下面我的编码
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;
}
string text=File.ReadAllText(outputFilePath);
text=DeleteBetween1(文本“,”);
公共字符串DeleteBetween1(字符串STR、字符串FirstString、字符串LastString)
{
string regularExpressionPattern1=“对上一个答案表示赞同。我已经在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.
}
void Main()
{
字符串xml=@“
当前选定记录:
[Font]Bold[/Font][DateStamp]2015/01/01 00:00:00[/DateStamp][Comment]这是一条评论。!![/Comment]
退出手指注册
[Font]常规[/Font][DateStamp]2014/012/01 00:00:00[/DateStamp][Comment]这是一条评论。!![/Comment]
";
var doc=XDocument.Load(新的StringReader(xml));
var nodes=doc.XPathSelectElements(“/root/data/value”);
foreach(节点中的var节点)
{
((XElement)节点).Value=string.Empty;
}
…在此处输出文档。
}
使用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;
}
}
}
这是一个resx文件(xml)。你能演示一下你在说什么吗,am bit losti尝试了这个方法,它返回了所有的值标记。我只想要我的表“data”中的值标记,因为你可以看到Regex Regex=new Regex(String.Format(@)(?表名为“data”我不想删除数据表中的值,请查看我的xml代码我有有效的xml.XmlDocument _doc=new XmlDocument();_doc.Load(outputFilePath);//filename string xmlcontents=_doc.InnerXml;您的方法甚至删除标记,我想删除标记之间的内容,因为它删除了标记。我使用的是linq方法