-
January 13th, 2010, 03:57 AM
#1
Parsing a string programmatically(C#)
Hi,
How to parse this string?
config = "<configuration><screen fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\" linesize=\"4\" theme_color1=\"-1518056\" theme_color2=\"-9674238\" /></configuration>";
into:
config = "<configuration><screen fadetimeout=\"3\" font=\"comic\" fontsize=\"12\" linesize=\"3\" theme_color1=\"-1513356\" theme_color2=\"-967538\" /></configuration>";
Thankx
-
January 13th, 2010, 04:06 AM
#2
Re: Parsing a string programmatically(C#)
Looks like XML, why not using the XML methods from .NET to access the values?
-
January 14th, 2010, 09:15 AM
#3
Re: Parsing a string programmatically(C#)
I cannot see the difference. Despite that, the second one doesn't appear as parsed the first one at all. What is your goal?
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 14th, 2010, 10:29 AM
#4
Re: Parsing a string programmatically(C#)
Please provide a better explanation of what you're trying to accomplish. Your example data does not show parsing at all- it just shows two different strings of XML code.
-
January 14th, 2010, 01:31 PM
#5
Re: Parsing a string programmatically(C#)
how do you like this?
Code:
int newFadetimeout = 3;
string config = "<configuration><screen fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\" linesize=\"4\" theme_color1=\"-1518056\" theme_color2=\"-9674238\" /></configuration>";
config = Regex.Replace(
config,
"(fadetimeout)=\"(\\d{1,})\"",
delegate(Match match)
{
return (match.Groups[1].Value + "=\"" + newFadetimeout + "\"");
//return (match.Groups[1].Value + "=\"" + (int.Parse(match.Groups[2].Value) + 2) + "\""); // or increase by 2
});
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
January 15th, 2010, 03:25 AM
#6
Re: Parsing a string programmatically(C#)
Because the data looks like XML, I would recommend to handle them as XML: create a XML document from them and than use XPath or LINQ to XML to manipulate it. But I still miss the intention.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|