如何使用C#的REGEX删除字符之间的空格,制表符和新行? [英] How can I remove the spaces, tabs, new lines between characters using c#'s REGEX?
本文介绍了如何使用C#的REGEX删除字符之间的空格,制表符和新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用C#从存储在文本文件中的以下字符串中删除">" and "<"
,">" and "</"
之间的空格,制表符,换行符以及类似<wiretype />
的空格?
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetReport xmlns="http://tempuri.org/">
<RequestContext xmlns="">
<userid>reds</userid><fcnumber>1</fcnumber><accountaccess /><wiretype /><currency /><accountheader>All</accountheader><clientname>Begum Noor</clientname><requestid>9999</requestid><ntid>reds</ntid>
</RequestContext>
<ReportParams>xyz</ReportParams>
</GetReport>
</soap:Body>
</soap:Envelope>
我尝试了以下操作,但并没有删除所有空格:
static void Main(string[] args)
{
string filename = args[0];
StringBuilder result = new StringBuilder();
if (System.IO.File.Exists(filename))
{
using (StreamReader streamReader = new StreamReader(filename))
{
String line;
Regex r = new Regex(@">\s+<");
while ((line = streamReader.ReadLine()) != null)
{
string newLine = r.Replace(line, @"><");
result.Append(newLine);
}
}
}
Console.WriteLine(result);
Console.ReadLine();
using (FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate))
{
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write(result);
streamWriter.Close();
fileStream.Close();
}
}
解决方案
为什么不使用:
XDocument xdoc = XDocument.Load(filename);
xdoc.Save(filename, SaveOptions.DisableFormatting);
它将删除您的xml
文档中的所有格式.有关更多详细信息,请参见 SaveOptions.DisableFormatting
. /p>
How can I remove the spaces, tab characters, new line characters between ">" and "<"
, ">" and "</"
and also the space between like <wiretype />
from the below string stored in a text file using C#?
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetReport xmlns="http://tempuri.org/">
<RequestContext xmlns="">
<userid>reds</userid><fcnumber>1</fcnumber><accountaccess /><wiretype /><currency /><accountheader>All</accountheader><clientname>Begum Noor</clientname><requestid>9999</requestid><ntid>reds</ntid>
</RequestContext>
<ReportParams>xyz</ReportParams>
</GetReport>
</soap:Body>
</soap:Envelope>
I tried the following but it didn't remove the all the spaces:
static void Main(string[] args)
{
string filename = args[0];
StringBuilder result = new StringBuilder();
if (System.IO.File.Exists(filename))
{
using (StreamReader streamReader = new StreamReader(filename))
{
String line;
Regex r = new Regex(@">\s+<");
while ((line = streamReader.ReadLine()) != null)
{
string newLine = r.Replace(line, @"><");
result.Append(newLine);
}
}
}
Console.WriteLine(result);
Console.ReadLine();
using (FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate))
{
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.Write(result);
streamWriter.Close();
fileStream.Close();
}
}
解决方案
Why don't you use:
XDocument xdoc = XDocument.Load(filename);
xdoc.Save(filename, SaveOptions.DisableFormatting);
it will remove all the formatting in your xml
document. See SaveOptions.DisableFormatting
for more details.
这篇关于如何使用C#的REGEX删除字符之间的空格,制表符和新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文