Archive for the 'C#' Category
In popular object-oriented languages such as Java and C#, they have a built-in method to each class that allows you to obtain a String representation of that class. They call these methods toString() or ToString(), respectively. You can use the default implementation, which basically gives you the name of the class (not usually very helpful). [...] Read more »
December 31st, 2008 | Posted in C#, Java, Python, Tip | 1 Comment
Suppose you have a Web resource that you want to download programmatically (as opposed to doing it manually from a Web browser) from C#. The first thing you need to do is send an HTTP request to the server with the URL (e.g. http://www.google.com). The server will then process the request and send an HTTP [...] Read more »
December 29th, 2008 | Posted in C#, Tip | No Comments
Let’s say you have an array of numbers and want to be able to find the mathematical mean of those numbers in C#. To my knowledge, the .NET Framework does not have a function to do this built in. (Please let me know if you know otherwise.) So I built a method that does this. [...] Read more »
December 28th, 2008 | Posted in C#, Math, Tip | 1 Comment
Let’s say you have an array of numbers (either doubles or integers) and want to be able to find the sum of these numbers. To my knowledge, there is not a built-in way to do this in the .NET Framework, so I created a little utility method that does this for doubles and for integers. [...] Read more »
December 28th, 2008 | Posted in C#, Math, Tip | 1 Comment
If you have an array of doubles (or floats) in C#, there is no built-in method (that I know of) to find the product of those numbers. The product is where you multiply all of the numbers together. I needed this, so I created a utility method that does this simple operation. See below.
public static [...] Read more »
December 27th, 2008 | Posted in C#, Math, Tip | No Comments
Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »
December 27th, 2008 | Posted in C#, Java, Python, Reflection, Tip | No Comments
Collections, such as List and ArrayList, in C# used to be able to contain any class. So you could implement the following code without a problem.
ArrayList oldWay = new ArrayList();
oldWay.Add("abc");
oldWay.Add(123);
oldWay.Add(new CustomWhatever());
But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to string [...] Read more »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
In the .NET Framework, there are two interfaces designed to allow you to iterate easily over collections of objects as you would typically do in a for loop. Many classes in the .NET Framework have implemented these interfaces and do their work behind the scenes so you don’t have to worry about how it is [...] Read more »
December 8th, 2008 | Posted in C#, Collections, Tip | No Comments
Please refer to this post for some background.
This series of posts has demonstrated how you can send XML-RPC requests to a WordPress server and get back responses. This allows you to do things like automatically post blog entries, modify them, delete them, etc. This is the type of thing that blogging editors do so you [...] Read more »
November 20th, 2008 | Posted in C#, XmlRpc | 2 Comments
Please refer to this post for some background.
Now that all the pieces are in place, a class called WordPressClient brings it all together. It has methods that correspond with some of the tasks you might try to accomplish (such as creating a new post, deleting a post, adding a category, etc.).
using System;
using System.Collections.Generic;
using System.Text;
namespace Piccolo.Common
{
public [...] Read more »
November 19th, 2008 | Posted in C#, XmlRpc | 1 Comment
Please refer to this post for some background.
The next class is used for actually sending and receiving XML-RPC messages over HTTP. I’ve also added some statements that write the requests and responses to Console.Out so you can see what they look like as they are going across the wire.
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
namespace [...] Read more »
November 19th, 2008 | Posted in C#, XmlRpc | No Comments
Please refer to this post for some background.
This section builds on the previous post about regular expressions. When the XML-RPC server sends a response, you need to be able to parse through the XML. The following class can be used for that purpose.
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Piccolo.Common
{
public class WordPressXmlParser
{
public static string[] ParseArray(string result)
{
result = [...] Read more »
November 18th, 2008 | Posted in C#, XmlRpc | No Comments
Please refer to this post for some background.
After you send a request to the XML-RPC server, it sends a response back to you in an XML structure. You have to parse out the values that are important (for example, whether the request was successful, a new post ID that was generated, etc.)
Again, the .NET Framework [...] Read more »
November 17th, 2008 | Posted in C#, XmlRpc | No Comments
Please refer to this post for some background.
When you’re sending requests to the server, you have to build text in XML. There are a few ways to do this in C#. One way is to use classes that were designed for this in the .NET Framework. However, I decided not to use these because they [...] Read more »
November 17th, 2008 | Posted in C#, XmlRpc | No Comments
Please refer to this post for some background.
OK, this post is going to talk about some of the plumbing. When you are sending requests to the server and when the server is replying with responses, you need to deal with key-value pairs. For example, userName = “steve” or password = “abc123″. C# has some classes [...] Read more »
November 15th, 2008 | Posted in C#, Collections, XmlRpc | No Comments
Please refer to this post for some background.
Using XML-RPC, you can create new blog posts, delete posts, edit posts, add categories, delete categories, etc.
XML-RPC operates over HTTP (the protocol we use to access a typical Web page). You send information in a request to the server, and the server replies with a response, telling you [...] Read more »
November 15th, 2008 | Posted in C#, XmlRpc | No Comments
Please refer to this post for some background on what I’m trying to do here.
Because it’s simple, I’m going to start with a class called WordPressBlogPost. This class holds information about a post–in this case, title, text, and categories. It’s not comprehensive but covers the core information you would need to know about a post.
namespace [...] Read more »
November 14th, 2008 | Posted in C#, XmlRpc | 1 Comment
This is the first in a series of posts about how to publish to a WordPress blog using an interface called XmlRpc that is supported by WordPress as well as other blogging platforms.
My motivation for writing these posts is that I have a hobby project that is written in the C# language, and I wanted [...] Read more »
November 14th, 2008 | Posted in C#, Java, Python, XmlRpc | 5 Comments