【发布时间】:2012-08-08 20:31:50
【问题描述】:
下面的函数没有返回我想要的节点的值,即“CompanyPolicyId”。我已经尝试了很多东西,但我仍然无法让它工作。有谁知道可能是什么问题?
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);
}
}
这是 XML 文档:
<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>
在过去的 6 个小时里,我一直在尝试解决这个问题。老实说,这很糟糕。
【问题讨论】:
-
你有没有通过代码..?试试看,看看 NULL 值出现在哪里..
-
这一行为空:var node = doc.DocumentElement.SelectSingleNode("/InsurancePolicy/Indentifiers/Identifier[@name='CompanyPolicyId']", mgr);
-
如果你把它改成读这样的东西会发生什么..? //标识符[@name='CompanyPolicyId']"
-
从namespacemanager中的namespace中移除http://
-
@DJKRAZE,现在我得到了节点变量的“元素,名称='标识符'”。
标签: c# xml xml-namespaces