Publishing to a WordPress Blog via XML-RPC (Part 6)
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 = RemoveOuterWrapper(result); result = result.Replace("<array><data>", "").Replace("</data></array>", "").Trim(); result = result.Replace("<value><string>", "").Replace("</string></value>", "").Replace(" ", ""); return Regex.Split(result, @"\s"); } public static string ParseSingleStringValue(string result) { return RemoveOuterWrapper(result).Replace("<string>", "").Replace("</string>", ""). Replace("<boolean>", "").Replace("</boolean>", ""); } public static bool ParseSingleBoolValue(string result) { return RemoveOuterWrapper(result).Replace("<boolean>", "").Replace("</boolean>", "") == "1"; } public static KeyValuePairs ParseStructValues(string result) { result = RemoveOuterWrapper(result); result = RegExHelper.RemovePatternBeforeAndAfter(result, "<struct>", "</struct>"); KeyValuePairs values = new KeyValuePairs(); foreach (string value in Regex.Split(result, "<member>")) { if (value.Trim() != string.Empty) { string modValue = value.Replace("</member>", ""); string[] nameValue = Regex.Split(modValue, "</name><value>"); string name = nameValue[0].Replace("<name>", ""); string value2 = nameValue[1]; value2 = RegExHelper.RemovePatternBeforeAndAfter(value2, ">", "<"); values.Add(name, value2); } } return values; } public static string RemoveOuterWrapper(string result) { return RegExHelper.RemovePatternBeforeAndAfter(result, @"<param>\s+<value>", @"</value>\s+</param>"); } } }
As with other portions of the code, this probably will not cover all tasks that you need to do. But it does for many different types of responses. If you need to process other types of responses, you can always extend it.
