Skip to content

Instantly share code, notes, and snippets.

@tomfanning
Created June 19, 2015 14:28
Show Gist options
  • Save tomfanning/cfc6cc11595fd60528ed to your computer and use it in GitHub Desktop.
Save tomfanning/cfc6cc11595fd60528ed to your computer and use it in GitHub Desktop.
Program to patch Enterprise.wsdl which Salesforce thinks is okay but causes Microsoft's wsdl.exe to emit non-working code
namespace FixEnterpriseWsdl
{
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
/// <summary>
/// Implements a fix for https://developer.salesforce.com/forums?id=906F0000000AiPEIA0
/// </summary>
class Program
{
static int Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Implements a fix for https://developer.salesforce.com/forums?id=906F0000000AiPEIA0");
Console.WriteLine("Expects a single command line parameter - a path to an Enterprise.wsdl file");
return -1;
}
if (!File.Exists(args[0]))
{
Console.WriteLine("File not found: " + args[0]);
return -1;
}
Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " started");
const string docNsUrl = "http://schemas.xmlsoap.org/wsdl/";
const string xsdNsUrl = "http://www.w3.org/2001/XMLSchema";
try
{
var doc = new XmlDocument();
doc.Load(args[0]);
var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("docns", docNsUrl);
nsManager.AddNamespace("xsd", xsdNsUrl);
XmlElement docElement = doc.DocumentElement;
XmlNode node = docElement.SelectSingleNode("docns:types/xsd:schema/xsd:complexType[@name='ListViewRecord']", nsManager);
if (node.ChildNodes.OfType<XmlElement>().Count() == 1)
{
// insert a node after the first one
var newNode = doc.CreateElement("xsd", "attribute", xsdNsUrl);
var nameAttribute = doc.CreateAttribute("name");
nameAttribute.Value = "tmp";
var typeAttribute = doc.CreateAttribute("type");
typeAttribute.Value = "xsd:string";
newNode.Attributes.Append(nameAttribute);
newNode.Attributes.Append(typeAttribute);
node.AppendChild(newNode);
doc.Save(args[0]);
Console.WriteLine("Fixed " + args[0]);
}
else
{
Console.WriteLine("Nothing to do");
}
return 0;
}
catch (Exception ex)
{
Console.WriteLine("{0}: {1}", ex.GetBaseException().GetType().Name, ex.GetBaseException().Message);
return -1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment