I’m writing some code that handles logging xml data and I would like to be able to replace the content of certain elements (eg passwords) in the document. I’d rather not serialize and parse the document as my code will be handling a variety of schemas.
Sample input documents:
doc #1:
<user> <userid>jsmith</userid> <password>myPword</password> </user>
doc #2:
<secinfo> <ns:username>jsmith</ns:username> <ns:password>myPword</ns:password> </secinfo>
What I’d like my output to be:
output doc #1:
<user> <userid>jsmith</userid> <password>XXXXX</password> </user>
output doc #2:
<secinfo> <ns:username>jsmith</ns:username> <ns:password>XXXXX</ns:password> </secinfo>
Since the documents I’ll be processing could have a variety of schemas, I was hoping to come up with a nice generic regular expression solution that could find elements with password in them and mask the content accordingly.
Can I solve this using regular expressions and C# or is there a more efficient way?
This problem is best solved with XSLT:
This will work for both inputs as long as you handle the namespaces properly.
Edit : Clarification of what I mean by "handle namespaces properly"
Make sure your source document that has the
nsname prefix has as namespace defined for the document like so: