2012-08-08 53 views
0

以下函数不返回所需节点的值,即“CompanyPolicyId”。我尝试了很多东西,但仍然无法实现。任何人都知道可能是什么问题?XML节点始终为空

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个小时。老实说,这很糟糕。

+0

你有没有通过代码..?试试看看NULL值在哪里出现.. – MethodMan 2012-08-08 20:33:36

+0

它在这一行是空的:var node = doc.DocumentElement.SelectSingleNode(“/ InsurancePolicy/Indentifiers/Identifier [@ name ='CompanyPolicyId']”,mgr); – Testifier 2012-08-08 20:34:10

+1

如果你改变它来阅读这样的事情会发生什么..? //标识符[@ name ='CompanyPolicyId']“ – MethodMan 2012-08-08 20:36:54

回答

1

尝试使用下面

XElement rootElement = XElement.Load(<url here>); 
string targetValue = 
    (string)rootElement.Elements("Identifier") 
    .Single(e => (string)e.Attribute("name") == "CompanyPolicyId") 
    .Attribute("value"); 

//Identifier[@name='CompanyPolicyId']" 

或不同的方法这是假设你希望能够按目标名称标识节点之一,而且你肯定那将会有一个这个名字的元素。如果不是这样,那么如果没有找到该节点,那么.Single调用将抛出一个异常。

如果您需要使用凭据并希望使用WebClient,则可以使用以下内容: (请注意,我没有执行异常处理,检查流的可用性,或以其他方式处理/关闭流,只是例如,如何得到它的 “工作”)

string uri = "> url here! <"; 
System.Net.WebClient wc = new System.Net.WebClient(); 
StreamReader sr = new StreamReader(wc.OpenRead(uri)); 
string xml = sr.ReadToEnd(); 
XElement rootElement = XElement.Parse(xml); 
string targetValue = 
    (string)rootElement.Elements("Identifier") 
    .Single(e => (string)e.Attribute("name") == "CompanyPolicyId") 
    .Attribute("value"); 
0

下面是简单的版本

[Test] 
    public void Test() 
    { 
     XElement root = XElement.Load(@"C:\1.xml"); 
     XElement identifier = GetIdentifierByName(root, "CompanyPolicyId"); 
     if (identifier == null) 
     { 
      return; 
     } 
     Console.WriteLine(identifier.Attribute("value")); 
    } 

    private static XElement GetIdentifierByName(XContainer root, string name) 
    { 
     return root.Descendants() 
      .Where(x => x.Name.LocalName == "Identifier") 
      .FirstOrDefault(x => x.Attribute("name").Value == name); 
    } 
} 

控制台输出BBBB