Xpath help needed for CRM newbie

R

roger

I am customizing MS CRM 3.0. I am fairly new to C# and know very
little about Xpath.
I need to read the the 4th line of the XML below (value node) and check
the type. If the type is "account", I need to change the type to
"contact" and replace the GUID with the value in my contactID variable.

Can anybody help this newbie?

Here is the XML:

<BusinessEntity xsi:type="DynamicEntity" Name="opportunity"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/crm/2006/WebServices">
<Properties>
</Properties><Property xsi:type="CustomerProperty"
Name="customerid">
<Value
type="account">{3434B094-9949-DB11-BA2F-0004765B94A0}</Value>
</Property>
<Property xsi:type="StringProperty" Name="name">
<Value>tester</Value>
</Property>
<Property xsi:type="CrmBooleanProperty"
Name="isrevenuesystemcalculated">
<Value>1</Value>
</Property>
<Property xsi:type="PicklistProperty" Name="opportunityratingcode">
<Value>2</Value>
</Property>
<Property xsi:type="OwnerProperty" Name="ownerid">
<Value
type="systemuser">{C2B731F4-6F16-DB11-A3CD-000472EB94C0}</Value>
</Property><Property xsi:type="StatusProperty" Name="statuscode">
<Value>1</Value>
</Property>
<Property type="LookupProperty" Name="defaultpricelevelid" >
<Value>{b9657b71-333d-db11-9bad-000446eb91a0}</Value>
</Property>
</Properties>
</BusinessEntity>

Thank you
 
S

Samuel R. Neff

You need to make sure you have a prefix in the XPath query. Also, the
xml posted has an extra </Properties> in it after the opening
properties.

HTH,

Sam


XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<BusinessEntity xsi:type='DynamicEntity'
Name='opportunity'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://schemas.microsoft.com/crm/2006/WebServices'>
<Properties>
<Property xsi:type='CustomerProperty'
Name='customerid'>
<Value
type='account'>{3434B094-9949-DB11-BA2F-0004765B94A0}</Value>
</Property>
<Property xsi:type='StringProperty' Name='name'>
<Value>tester</Value>
</Property>
<Property xsi:type='CrmBooleanProperty'
Name='isrevenuesystemcalculated'>
<Value>1</Value>
</Property>
<Property xsi:type='PicklistProperty'
Name='opportunityratingcode'>
<Value>2</Value>
</Property>
<Property xsi:type='OwnerProperty' Name='ownerid'>
<Value
type='systemuser'>{C2B731F4-6F16-DB11-A3CD-000472EB94C0}</Value>
</Property><Property xsi:type='StatusProperty'
Name='statuscode'>
<Value>1</Value>
</Property>
<Property type='LookupProperty' Name='defaultpricelevelid' >
<Value>{b9657b71-333d-db11-9bad-000446eb91a0}</Value>
</Property>
</Properties>
</BusinessEntity>");
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("crm",
"http://schemas.microsoft.com/crm/2006/WebServices");

XmlNode value =
doc.SelectSingleNode("/crm:BusinessEntity/crm:properties/crm:property/crm:Value[@type='account']",
mgr);

if (value == null)
{
Console.WriteLine("No account found");
}
else
{
Guid oldGuid = new Guid(value.InnerText);
Guid newGuid = Guid.NewGuid();
value.InnerText = newGuid.ToString();
Console.WriteLine("Changed {0} to {1}", oldGuid, newGuid);
}


------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top