2012-08-08 69 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