+ Start a Discussion

C# XML manipulation possibilities

I know this is a little out of sforce's scope, but i'm trying to improve code efficiency.

Is it possible to convert/cast the sObject's XmlElement array into an MSXML parser object or some similar construct so that the nodes are accessible via XPath strings?  I'm unsure of any other way of locating nodes in sObjects beyond looping through the names which is very messy.  I'm using C#.



If you twiddle the right bits (you'll need to check the docs for the exact syntax), you can get .NET to give you an XmlElement instead of de-serializing to objects, what you have at that point is basically a DOM fragment, and you can run XPath etc over it etc.
Michael SMichael S

This will convert any object that supports serialization (like all of the SForce objects) into an XmlElement.

Michael S. Scherotter

Business Solutions Architect

Mindjet LLC

static XmlElement ToXMLElement(object rObject)


XmlSerializer ser = new XmlSerializer(rObject.GetType() );

StringWriter writer = new StringWriter();

ser.Serialize( writer, rObject);

XmlDocument doc = new XmlDocument();


return doc.DocumentElement;


It will, but you can get .NET to give you an XmlElement directly, no need to pay the overhead of going from XML -> objects and back to XML.
Michael SMichael S

I know that you can do this by manipulating the generated C# from the web reference.  Is there a way to do this without changing the generated code?



thanks guys, very helpful




well those solutions work great, what would be the equivalent operations in java?



Hi upNcommN,

I think you can do this with the org.apache.axis.encoding.ser package.  I've not done it myself, but I'd start there.