Publishing to a WordPress Blog via XML-RPC (Part 5)
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 has built-in classes to do this type of parsing, but I felt it was more straightforward and compatible with other programming languages to do it using a technique called regular expressions. A regular expression is a text pattern that follows specific rules, and it’s used to find subsets of text that match it. (Well, it is more complicated than that, but I won’t go into details here.) For parsing XML-RPC responses, I created a simple class that helps remove text before and after locations in the text that match specific regular expressions. This allows you to remove the redundant parts of the XML and focus on the key parts.
Here is the class that does this:
using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace Piccolo.Common { public class RegExHelper { public static string RemovePatternAndBefore(string input, string pattern) { Match match = Regex.Match(input, pattern, RegexOptions.IgnorePatternWhitespace); if (match.Success) return input.Substring(match.Index).Remove(0, match.Value.Length).Trim(); return input; } public static string RemovePatternAndAfter(string input, string pattern) { MatchCollection matches = Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace); if (matches.Count > 0) { Match match = matches[0]; if (match.Success) return input.Substring(0, match.Index).Trim(); } return input; } public static string RemovePatternBeforeAndAfter(string input, string beforePattern, string afterPattern) { input = RemovePatternAndBefore(input, beforePattern); return RemovePatternAndAfter(input, afterPattern); } } }
Here is a set of nUnit tests that test the functionality of this class and illustrate how you might invoke it.
using System; using System.Collections.Generic; using System.Text; using NUnit.Framework; namespace Piccolo.Common.Tests { [TestFixture] public class RegExHelperTests { [Test] public void RemovePatternAndBefore() { Assert.AreEqual("de", RegExHelper.RemovePatternAndBefore("abcde", "bc")); Assert.AreEqual("", RegExHelper.RemovePatternAndBefore("abcde", "de")); Assert.AreEqual("abcde", RegExHelper.RemovePatternAndBefore("abcde", "fg")); Assert.AreEqual("", RegExHelper.RemovePatternAndBefore("", "")); } [Test] public void RemovePatternAndAfter() { Assert.AreEqual("a", RegExHelper.RemovePatternAndAfter("abcde", "bc")); Assert.AreEqual("abc", RegExHelper.RemovePatternAndAfter("abcde", "de")); Assert.AreEqual("abcde", RegExHelper.RemovePatternAndAfter("abcde", "fg")); Assert.AreEqual("", RegExHelper.RemovePatternAndAfter("", "")); } [Test] public void RemovePatternBeforeAndAfter() { Assert.AreEqual("c", RegExHelper.RemovePatternBeforeAndAfter("abcde", "b", "d")); Assert.AreEqual("bcd", RegExHelper.RemovePatternBeforeAndAfter("abcde", "a", "e")); Assert.AreEqual("abcde", RegExHelper.RemovePatternBeforeAndAfter("abcde", "yz", "fg")); Assert.AreEqual("", RegExHelper.RemovePatternBeforeAndAfter("", "", "")); } } }