2010-03-24 145 views
6

我有以下的功能,我使用刪除字符\ 04從我的xmlString,但我無法找到我需要做什麼改變,以避免刪除從我的結尾標籤中。這是我所得到的,當我運行這個功能正則表達式解析XML在.NET

<ARR>20080625<ARR><DEP>20110606<DEP><PCIID>626783<PCIID><NOPAX>1<NOPAX><TG><TG><HASPREV>FALSE<HASPREV><HASSUCC>FALSE<HASSUCC> 

任何人可以幫助我找出我需要在我的表情變化,以保持結束標記爲</tag>

Private Function CleanInput(ByVal inputXML As String) As String 
    ' Note - This will perform better if you compile the Regex and use a reference to it. 
    ' That assumes it will still be memory-resident the next time it is invoked. 
    ' Replace invalid characters with empty strings. 
    Return Regex.Replace(inputXML, "[^><\w\[email protected]]", "") 
End Function 
+0

這不會刪除' '\ 0''和'' 從你的字符串\ 04''字符,而是消除一切,除了幾個字符(''<', '>,空白,'.','@'和'-')。另外,提供輸出的輸入是什麼? – Thomas 2010-03-24 16:11:47

+0

你可以發佈一行或兩行的輸入到這個函數的樣子嗎? – 2010-03-24 16:12:16

+0

@Thomas,'\ w'是單詞字符,而不是空格。 – Joel 2010-03-24 16:14:17

回答

4
Private Function CleanInput(ByVal inputXML As String) As String 
    Return Regex.Replace(inputXML, "[^/><\w\[email protected]]", "") 
    ' --------------------------------^ 
End Function 

但是,由於您的目標僅僅是刪除了\04\00,因此僅限於替換它們更安全。

Private Function CleanInput(ByVal inputXML As String) As String 
    Return Regex.Replace(inputXML, "[\4\0]", "") 
End Function 
+0

非常感謝!大家爲你的意見。我現在得到一個乾淨的XML。 – Tony 2010-03-24 16:35:32