(TLDR at the bottom)
We have a legacy system that has implemented its own XML reader/writer. The problem is that it allows a literal "&" inside a property value.
<SB nae="Name" net="HV & DD"/>
When I am reading the data using XDocument.Parse() method, this fails of course. I am looking at ways of sanitizing the data.
I am attempting to use regex to identify cases where this is happening.
To illustrate, consider this:
&(?!amp;)
This will identify ampersand with a negative lookahead to ensure it isn't actually a correctly escaped ampersand. When I have identified these cases, I can substitute with a proper &
Of course, there is a problem that this will match other escaped character such > < " etc, so I need to unmatch those as well. Maybe using a more general form, like a regex unmatching ampersand followed by 2-4 characters and then semicolon.
But my worry is that there are other cases for ampersands that I am not thinking of and that are not represented in the few samples I have got. I am looking for a safe way that will not mess up proper xml.
TLDR: How do I identify ampersands that are not part of proper xml, but are cases of unescaped ampersands in property values?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…