+ Start a Discussion
upNcommNupNcommN 

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#.

thanks

aj

SuperfellSuperfell
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();

doc.LoadXml(writer.ToString());

return doc.DocumentElement;

}

SuperfellSuperfell
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?

Michael

upNcommNupNcommN

thanks guys, very helpful

 

aj

upNcommNupNcommN

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

aj

DevAngelDevAngel

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.