2010-01-28 133 views
3

我以下面的格式從Web服務中獲取XML,並且在使用它之前我想清除它(刪除多餘的「\」和「\ n」字符)。我目前使用下面的正則表達式進行匹配。然而,只有「\ n」字符被清除,而在相等和雙引號之間的「\」字符仍然存在。如何使用.NET Regex庫匹配並刪除反斜槓「」和「 n」字符?

你建議我做什麼?

private string ValidateXml(string dirtyXml) { 
    Regex regex = new Regex(@"[\\\][\n]"); 
    var cleanXml = regex.Replace(dirtyXml, ""); 
    return cleanXml; 
} 

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<ISBNdb server_time=\"2010-01-28T11:31:08Z\">\n<BookList total_results=\"1\" page_size=\"10\" page_number=\"1\" shown_results=\"1\">\n<BookData book_id=\"quantitative_techniques\" isbn=\"0826458548\" isbn13=\"9780826458544\">\n<Title>Quantitative techniques</Title>\n<TitleLong></TitleLong>\n<AuthorsText>Terry Lucey</AuthorsText>\n<PublisherText publisher_id=\"continuum\">London : Continuum, 2002.</PublisherText>\n</BookData>\n</BookList>\n</ISBNdb>\n" 
+0

你如何查看輸出(cleanXml)XML?這看起來像一個正常逃逸的C#字符串。 – Lazarus 2010-01-28 12:35:20

+0

是的拉撒路,cleanXml是我輸出使用,但「\」的presist。 – simplyme 2010-01-28 12:39:38

+0

我認爲當你發佈這個時,你從你的正則表達式中放下了''''。 – 2010-01-28 14:43:58

回答

3

的問題仍然是不明確的:如果你寫的XML字符串(你試圖清理之前)到控制檯,你有沒有看到你在上面發佈的內容,所有那些\"\n序列?顯示的字符串是否以引號開頭和結尾?如果是這樣,您可能想要刪除開始和結束引號以及所有反斜槓,並且如果任何反斜槓後跟一個'n',您也希望刪除它。下面是一些代碼來演示:

static void Main(string[] args) 
{ 
    string dirtyXml = @"""<?xml version=\""1.0\"" encoding=\""UTF-8\""?>\n\n<ISBNdb server_time=\""2010-01-28T11:31:08Z\"">\n<BookList total_results=\""1\"" page_size=\""10\"" page_number=\""1\"" shown_results=\""1\"">\n<BookData book_id=\""quantitative_techniques\"" isbn=\""0826458548\"" isbn13=\""9780826458544\"">\n<Title>Quantitative techniques</Title>\n<TitleLong></TitleLong>\n<AuthorsText>Terry Lucey</AuthorsText>\n<PublisherText publisher_id=\""continuum\"">London : Continuum, 2002.</PublisherText>\n</BookData>\n</BookList>\n</ISBNdb>\n"""; 
    Console.WriteLine(dirtyXml); 
    Console.WriteLine(); 
    Console.WriteLine(Regex.Replace(dirtyXml, @"^""|""$|\\n?", "")); 
} 

輸出:

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n<ISBNdb server_time=\"2010-01-28T11:31:08Z\">\n<BookList total_results=\"1\" page_size=\"10\" page_number=\"1\" shown_results=\"1\">\n<BookData book_id=\"quantitative_techniques\" isbn=\"0826458548\" isbn13=\"9780826458544\">\n<Title>Quantitative techniques</Title>\n<TitleLong></TitleLong>\n<AuthorsText>Terry Lucey</AuthorsText>\n<PublisherText publisher_id=\"continuum\">London : Continuum, 2002.</PublisherText>\n</BookData>\n</BookList>\n</ISBNdb>\n"

<?xml version="1.0" encoding="UTF-8"?><ISBNdb server_time="2010-01-28T11:31:08Z"><BookList total_results="1" page_size="10" page_number="1" shown_results="1"><BookData book_id="quantitative_techniques" isbn="0826458548" isbn13="9780826458544"><Title>Quantitative techniques</Title><TitleLong></TitleLong><AuthorsText>Terry Lucey</AuthorsText><PublisherText publisher_id="continuum">London : Continuum, 2002.</PublisherText></BookData></BookList></ISBNdb>

這是否準確地反映你開始用什麼,你想結束了呢?

+0

艾倫現貨。非常感謝。 – simplyme 2010-01-29 13:53:22

0

你並不真的需要這樣的正則表達式,你可以使用一些對String.Replace的調用。

這應該做的伎倆:

var cleanXml = dirtyXml.Replace("\\n", "").Replace("\\\"", "\""); 
+0

嘿尼克,它不完全解決它。人物仍然在那裏 – simplyme 2010-01-28 12:50:38

1

你的正則表達式是有點古怪,它會匹配以下內容:

  • \\單個反斜槓字符
  • \[單[字符
  • ]單個字符
  • \n換行符

下面的正則表達式將匹配你所描述的:

@"\\n?" 

它匹配任何文字\n\。請注意,即使沒有引號後面的反斜槓也會匹配。要僅匹配反斜槓後跟一個報價,您可以使用此模式:

@"(\\n)|(\\(?=""))" 
+0

好抓!我原本讀爲'[\\\] [\ n]'作爲兩個角色課程,但你說得對,它只有一個。 – 2010-01-28 14:49:03

+0

我建議將您的正則表達式編寫爲字符串文字以避免混淆。我不認爲假設所有讀者都熟悉C#的逐字字符串是安全的,任何假設你的意思是「新正則表達式」(「\\ n?」)的人都會非常困惑。 :-) – 2010-01-28 15:10:50

+0

感謝您的建議,更正。 – 2010-01-28 23:03:53

0

它看起來像是你要|在該代碼說比賽要麼\ n或\

試試這個

[\\][n]|[\\]