Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to remove password='efgh' from the following string. How can I do that using c#.

XML
<?xml version="1.0"?><wql host='192.168.1.127' username='abcd' password='efgh' Type='XYZ'>
<query id='0.' ns='root\cimv2' devicetype='123'><![CDATA[select * from abc]]>
</query></wql>


Regards
Sebastian
Posted

You can use RegEx (regular expressions). I am not an expert with RegEx, but there is information at http://www.knowdotnet.com/articles/regereplacementstrings.html[^]. The regular expression you would need is "password=\'[a-z]+\'". Here is some code to show that this works:
C#
string x = "sdlkfsdal password='edere' dfldskflkds";
 Regex rx = new Regex("password=\'([a-z]+)\'");
var r = rx.Match(x);
if (r.Groups.Count > 0)
{
    var g1 = r.Groups[0]; // = password='edere'
    var g2 = r.Groups[1]; // = edere
}


Notice I put () around the "[a-z]+", which allows you to get the actual password.
 
Share this answer
 
v2
Comments
Sebastian T Xavier 22-Mar-12 5:16am    
very nice one
Clifford Nelson 22-Mar-12 11:13am    
I was not sure it was what you were looking for, glad it did the trick.
Try this :
C#
string output = xmlstring.Replace("password='efgh'","");
 
Share this answer
 
Comments
Sebastian T Xavier 22-Mar-12 3:52am    
ha ha ha... lazy solution. But the length / value of password will wary every time, so what to do?
Sergey Alexandrovich Kryukov 22-Mar-12 3:54am    
Damn, this is XML, parse it! There are three ways of doing it.
--SA
Sergey Alexandrovich Kryukov 22-Mar-12 3:57am    
And this answer is the direct result of your question, literally. What else could you expect. This is me who starts most of the answers with "The question makes no sense"; many other try to just answer, I don' really know why though... :-)
--SA
Mehdi Gholam 22-Mar-12 4:01am    
Well you did not specify anything else.
Sebastian T Xavier 22-Mar-12 4:00am    
pardon me.... please forget the XML , consider this as a simple string. then how we can do it? I want to store the whole string except the password in DB. password , username & host will be different every time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900