The following function does not return the value of the node that I want, which is “CompanyPolicyId”. I have tried so many things and I still cannot get it to work. Anyone know what might be the issue?
public void getpolicy(string rootURL, string policyNumber)
{
string basePolicyNumber = policyNumber.Remove(policyNumber.Length - 2);
basePolicyNumber = basePolicyNumber + "00";
using (WebClient client = new WebClient())
{
NetworkCredential credentials = new NetworkCredential();
credentials.UserName = AppVars.Username;
credentials.Password = AppVars.Password;
client.Credentials = credentials;
try
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(client.DownloadString(rootURL + basePolicyNumber));
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("zzzlocal", "http://com.zzz100.policy.data.local");
// Select the Identifier node with a 'name' attribute having an 'id' value
var node = doc.DocumentElement.SelectSingleNode("/InsurancePolicy/Indentifiers/Identifier[@name='CompanyPolicyId']", mgr);
if (node != null && node.Attributes["value"] != null)
{
// Pick out the 'value' attribute's value
var val = node.Attributes["value"].Value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here’s the XML document:
<InsurancePolicy xmlns:zzzlocal="com.zzz100.policy.data.local" schemaVersion="2.7" variant="multiterm">
<Identifiers>
<Identifier name="VendorPolicyId" value="AAAA"/>
<Identifier name="CompanyPolicyId" value="BBBB"/>
<Identifier name="QuoteNumber" value="CCCC"/>
<Identifier name="pxServerIndex" value="DDDD"/>
<Identifier name="PolicyID" value="EEEE"/>
</Identifiers>
</InsurancePolicy>
I’ve been trying to fix this problem for the last 6 hours. Honestly, this sucks.
Try to use this
or a different approach below
This assumes that you want to be able to target one of the Identifier nodes by name, and that you are sure that there will be an element WITH that name. If that is not true, then the .Single call will throw an Exception if that node is not found.
If you needed to use credentials and wanted to use a WebClient, then you could use the following:
(Note, I have done no exception handling, checking for stream availability, or otherwise disposed/closed the streams, just an example of how to get it to “work”)