dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Parsing a string programmatically(C#)

Hybrid View

  1. #1
    Join Date
    Jan 2010
    Posts
    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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Parsing a string programmatically(C#)

    Looks like XML, why not using the XML methods from .NET to access the values?

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  4. #4
    Join Date
    Dec 2008
    Posts
    144

    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.

  5. #5
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    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

  6. #6
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)