正则表达式解析XML在.NET
问题描述:
我有以下的功能,我使用删除字符\ 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
答
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
这不会删除' '\ 0''和'' 从你的字符串\ 04''字符,而是消除一切,除了几个字符('',空白,'.','@'和'-')。另外,提供输出的输入是什么? – Thomas 2010-03-24 16:11:47
你可以发布一行或两行的输入到这个函数的样子吗? – 2010-03-24 16:12:16
@Thomas,'\ w'是单词字符,而不是空格。 – Joel 2010-03-24 16:14:17