C# XML文件中的C正则表达式
我有一个XML文件,多次包含以下字符串: 请尝试以下操作:C# XML文件中的C正则表达式,c#,regex,xml,C#,Regex,Xml,我有一个XML文件,多次包含以下字符串: 请尝试以下操作: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.IO; namespace ConsoleApplication1 { class Program { const string F
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.txt";
static void Main(string[] args)
{
string input = File.ReadAllText(FILENAME);
string pattern = @"(?'start'@\()(?'middle'[^\)]+)(?'end'\))";
string output = Regex.Replace(input, pattern, "${middle}");
}
}
}
这是使用您的数据的输出
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Alarms>
<AlarmList>
<Alarm>
<Name Device="" Variable="" Area="1" ThresholdExclusive="0" Enabled="1" OnQualityGood="1" VariableDuration="0" EnableVariable="" EnableDispMsg="" Hysteresis="0" EventsCache="1024"></Name>
<ThresholdList>
<Threshold>
<Name Area="" Title="My - Searched - String" Help="" DurationFormat="" ReadAccessLevel="4294901760" WriteAccessLevel="4294901760">On</Name>
<Execution Condition="2" Threshold="1" ThresholdVar="" ThresholdLow="0" ThresholdVarLow="" VariableStatus="" Severity="10" SeverityVar="" SecDelay="0" RunCommandAtServer="0"/>
<Commands/>
<CommandsOn/>
<CommandsAck/>
<CommandsReset/>
<CommandsOff/>
<Style BackColor="4294967295" TextColor="65535" BlinkBackColor="4294967295" BlinkTextColor="4294967295" Print="1" Log="1" BlinkOnNewAlarm="0" VarTimeStamp="0" SupportAck="0" SupportReset="0" SupportResetConditionOn="0" BmpFile="" SndFile="" BeepEnabled="0" SpeechEnabled="0" RepeatSpeechEverySec="0" EnableSpeechVariable="" PlaysoundContinuosly="0" CommentOnAck="0"/>
<Recipient Attachment="" DispatchingText=""/>
<SendEmail SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendVoice SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendSMS SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendFax SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendAdminAlert SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendMessenger SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<ScriptCode StartSel="0" SelLength="0" OutStatusBar="1" OutLog="1" OutPrinter="1">
</ScriptCode>
</Threshold>
</ThresholdList>
</Alarm>
</AlarmList>
</Alarms>
对jdweng最后一条评论的答复: 是的,我试过你的代码。我也试着把它和Tim的答案混在一起,Tim说要使用XDocument。 我现在尝试的是:
XmlReader xr = new XmlTextReader(Globals.AlrFile);
lblMatches.Text = "";
while (xr.Read())
{
if(xr.NodeType == XmlNodeType.Element)
{
if(xr.AttributeCount > 0)
{
while(xr.MoveToNextAttribute())
{
string input = Convert.ToString(xr.Value);
string pattern = @"(?'start'@\()(?'middle'[^\)]+)(?'end'\))";
string output = Regex.Replace(input, pattern, "${middle}");
File.AppendAllText(Globals.AlrFileNeu, output + Environment.NewLine);
}
}
}
}
xr.Close();
但我的xml输入是:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Alarms>
<AlarmList>
<Alarm>
<Name Device="" Variable="" Area="1" ThresholdExclusive="0" Enabled="1" OnQualityGood="1" VariableDuration="0" EnableVariable="" EnableDispMsg="" Hysteresis="0" EventsCache="1024"></Name>
<ThresholdList>
<Threshold>
<Name Area="" Title="@(My) - @(Searched) - @(String)" Help="" DurationFormat="" ReadAccessLevel="4294901760" WriteAccessLevel="4294901760">On</Name>
<Execution Condition="2" Threshold="1" ThresholdVar="" ThresholdLow="0" ThresholdVarLow="" VariableStatus="" Severity="10" SeverityVar="" SecDelay="0" RunCommandAtServer="0"/>
<Commands/>
<CommandsOn/>
<CommandsAck/>
<CommandsReset/>
<CommandsOff/>
<Style BackColor="4294967295" TextColor="65535" BlinkBackColor="4294967295" BlinkTextColor="4294967295" Print="1" Log="1" BlinkOnNewAlarm="0" VarTimeStamp="0" SupportAck="0" SupportReset="0" SupportResetConditionOn="0" BmpFile="" SndFile="" BeepEnabled="0" SpeechEnabled="0" RepeatSpeechEverySec="0" EnableSpeechVariable="" PlaysoundContinuosly="0" CommentOnAck="0"/>
<Recipient Attachment="" DispatchingText=""/>
<SendEmail SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendVoice SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendSMS SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendFax SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendAdminAlert SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendMessenger SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<ScriptCode StartSel="0" SelLength="0" OutStatusBar="1" OutLog="1" OutPrinter="1">
</ScriptCode>
</Threshold>
</ThresholdList>
</Alarm>
</AlarmList>
</Alarms>
我的问题是,我只需要Title属性中间的文本,而且我有多个报警元素要处理。是否使用RegEx搜索XML?IMO,最好将XML转储到XDocument中,将代码段中的字符串提取到集合中,它们是Title属性的值,然后对集合中的字符串执行正则表达式。您需要显示您已尝试的内容,提供足够的代码来复制您的问题,请看“如何提问”部分。谢谢你的回答,但我的问题是我不知道字符串是什么样子,它们总是不同的。你试过代码了吗?我只是删除@和结束语。我刚用你的绳子测试过。输入可以是任何内容。是的,我必须遍历文件,其中在搜索字符串之前和之后是其他属性。显然,您没有尝试代码,如果您尝试了,您会看到它是有效的。我认为您需要阅读关于RegEx的文档才能真正理解我所做的事情:我用您的xml文件更新了我的代码。代码是有效的。看看我的输出,你不需要使用XDocument。您将结果附加到原始结果上,结果变得一团糟。看看我在上面发布的结果。
XmlReader xr = new XmlTextReader(Globals.AlrFile);
lblMatches.Text = "";
while (xr.Read())
{
if(xr.NodeType == XmlNodeType.Element)
{
if(xr.AttributeCount > 0)
{
while(xr.MoveToNextAttribute())
{
string input = Convert.ToString(xr.Value);
string pattern = @"(?'start'@\()(?'middle'[^\)]+)(?'end'\))";
string output = Regex.Replace(input, pattern, "${middle}");
File.AppendAllText(Globals.AlrFileNeu, output + Environment.NewLine);
}
}
}
}
xr.Close();
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Alarms>
<AlarmList>
<Alarm>
<Name Device="" Variable="" Area="1" ThresholdExclusive="0" Enabled="1" OnQualityGood="1" VariableDuration="0" EnableVariable="" EnableDispMsg="" Hysteresis="0" EventsCache="1024"></Name>
<ThresholdList>
<Threshold>
<Name Area="" Title="@(My) - @(Searched) - @(String)" Help="" DurationFormat="" ReadAccessLevel="4294901760" WriteAccessLevel="4294901760">On</Name>
<Execution Condition="2" Threshold="1" ThresholdVar="" ThresholdLow="0" ThresholdVarLow="" VariableStatus="" Severity="10" SeverityVar="" SecDelay="0" RunCommandAtServer="0"/>
<Commands/>
<CommandsOn/>
<CommandsAck/>
<CommandsReset/>
<CommandsOff/>
<Style BackColor="4294967295" TextColor="65535" BlinkBackColor="4294967295" BlinkTextColor="4294967295" Print="1" Log="1" BlinkOnNewAlarm="0" VarTimeStamp="0" SupportAck="0" SupportReset="0" SupportResetConditionOn="0" BmpFile="" SndFile="" BeepEnabled="0" SpeechEnabled="0" RepeatSpeechEverySec="0" EnableSpeechVariable="" PlaysoundContinuosly="0" CommentOnAck="0"/>
<Recipient Attachment="" DispatchingText=""/>
<SendEmail SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendVoice SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendSMS SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendFax SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendAdminAlert SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<SendMessenger SendON="0" SendACK="0" SendRESET="0" SendOFF="0"/>
<ScriptCode StartSel="0" SelLength="0" OutStatusBar="1" OutLog="1" OutPrinter="1">
</ScriptCode>
</Threshold>
</ThresholdList>
</Alarm>
</AlarmList>
</Alarms>
1
0
1
1
0
0
1024
My - Searched - String
4294901760
4294901760
2
1
0
10
0
0
4294967295
65535
4294967295
4294967295
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
1