Multiple web service references sharing types


To share types between more web service references, generate proxy classes manual with the wsdl.exe (for example in a Visual Studio command prompt) like this:

wsdl /sharetypes WebServiceURL1 WebServiceURL2 ... WebServiceURLn
/o:OutputFileName.cs /n:NamespaceName

Just add this .cs file to your project and use the generated namespace's classes, you should't add any web reference. The types which are the same on more web services will be generated only once to a common namespace. This is very useful, because you needn't copy a lot of properties...

Posted on 22:05 by csharper and filed under , , , , , | 1 Comments »

XML syntax highlighting in a rich textbox


Source: http://www.codeproject.com/KB/cs/RTFSyntaxColour.aspx

private void AddColouredText(string strTextToAdd)
{
    //Use the RichTextBox to create the initial RTF code
    txtbox.Clear();
    txtbox.AppendText(strTextToAdd);
    string strRTF = txtbox.Rtf;
    txtbox.Clear();
 
    /* 
     * ADD COLOUR TABLE TO THE HEADER FIRST 
     * */
 
    // Search for colour table info, if it exists (which it shouldn't)
    // remove it and replace with our one
    int iCTableStart = strRTF.IndexOf("colortbl;");
 
    if (iCTableStart != -1) //then colortbl exists
    {
        //find end of colortbl tab by searching
        //forward from the colortbl tab itself
        int iCTableEnd = strRTF.IndexOf('}', iCTableStart);
        strRTF = strRTF.Remove(iCTableStart, iCTableEnd - iCTableStart);
 
        //now insert new colour table at index of old colortbl tag
        strRTF = strRTF.Insert(iCTableStart,
            // CHANGE THIS STRING TO ALTER COLOUR TABLE
            "colortbl ;\\red255\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}");
    }
 
    //colour table doesn't exist yet, so let's make one
    else
    {
        // find index of start of header
        int iRTFLoc = strRTF.IndexOf("\\rtf");
        // get index of where we'll insert the colour table
        // try finding opening bracket of first property of header first                
        int iInsertLoc = strRTF.IndexOf('{', iRTFLoc);
 
        // if there is no property, we'll insert colour table
        // just before the end bracket of the header
        if (iInsertLoc == -1) iInsertLoc = strRTF.IndexOf('}', iRTFLoc) - 1;
 
        // insert the colour table at our chosen location                
        strRTF = strRTF.Insert(iInsertLoc,
            // CHANGE THIS STRING TO ALTER COLOUR TABLE
            "{\\colortbl ;\\red128\\green0\\blue0;\\red0\\green128\\blue0;\\red0\\green0\\blue255;}");
    }
 
    /*
     * NOW PARSE THROUGH RTF DATA, ADDING RTF COLOUR TAGS WHERE WE WANT THEM
     * In our colour table we defined:
     * cf1 = red  
     * cf2 = green
     * cf3 = blue             
     * */
 
    for (int i = 0; i < strRTF.Length; i++)
    {
        if (strRTF[i] == '<')
        {
            //add RTF tags after symbol 
            //Check for comments tags 
            if (strRTF[i + 1] == '!')
                strRTF = strRTF.Insert(i + 4, "\\cf2 ");
            else
                strRTF = strRTF.Insert(i + 1, "\\cf1 ");
            //add RTF before symbol
            strRTF = strRTF.Insert(i, "\\cf3 ");
 
            //skip forward past the characters we've just added
            //to avoid getting trapped in the loop
            i += 6;
        }
        else if (strRTF[i] == '>')
        {
            //add RTF tags after character
            strRTF = strRTF.Insert(i + 1, "\\cf0 ");
            //Check for comments tags
            if (strRTF[i - 1] == '-')
            {
                strRTF = strRTF.Insert(i - 2, "\\cf3 ");
                //skip forward past the 6 characters we've just added
                i += 8;
            }
            else
            {
                strRTF = strRTF.Insert(i, "\\cf3 ");
                //skip forward past the 6 characters we've just added
                i += 6;
            }
        }
    }
    txtbox.Rtf = strRTF;
}

Posted on 12:36 by csharper and filed under , , , | 1 Comments »

Add a connection string to app.config runtime


Add the System.Configuration reference to your project.

Using this namespace in your code.

And use this code segment to add the new connection string:

Configuration myConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
myConfig.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("ConnStr1", strConn));
myConfig.Save();

Posted on 12:14 by csharper and filed under , , , | 0 Comments »