2014-02-20 73 views
0

我有一個名爲app.exe.config的.config文件,我想更新一個名爲地址存在於端點標記內的特定屬性值。我嘗試了下面的代碼,但是不能得到什麼問題。我是vb.net的新手。請幫助。更新xml文件(.config文件)中的屬性值

<?xml version="1.0"?> 
<configuration> 
<startup><supportedRuntime version="v2.0.50727"/></startup> 
    <system.serviceModel> 
    <client> 
     <endpoint address="valuetobeupdated" 
      binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_CompassSMSService" 
      contract="CompassSMSService.CompassSMSService" name="BasicHttpBinding_CompassSMSService" /> 
    </client> 
    <bindings> 
     <basicHttpBinding> 
     <binding name="BasicHttpBinding_CompassSMSService" closeTimeout="00:01:00" openTimeout="00:01:00" 
      receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" 
      bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
      messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
      useDefaultWebProxy="true"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
       maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <security mode="None"> 
      <transport clientCredentialType="None" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="UserName" algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </basicHttpBinding> 
    </bindings> 
    </system.serviceModel> 
</configuration> 

我用下面的代碼,但它無法

Dim str As String = "Demo" 
     Dim doc As XmlDocument = New XmlDocument() 
     doc.Load("C:\Users\e554\Desktop\PAC\app.exe.config") 
     'Dim formId As XmlAttribute 
     For Each Attribute As XmlAttribute In doc.DocumentElement.Attributes 
      MessageBox.Show(Attribute.Value) 
     Next 

回答

1

您可以使用XPath語法來獲得端點元素的地址屬性,例如:

Dim str As String = "Demo" 
Dim doc As XmlDocument = New XmlDocument() 
doc.Load("C:\Users\e554\Desktop\PAC\app.exe.config") 
Dim endpoint = doc.SelectSingleNode("configuration\system.serviceModel\client\endpoint") 
Dim address = endpoint.Attributes["address"].Value; 
MessageBox.Show(address) 

注意SelectSingleNode將獲得在功能參數中提供的第一個節點匹配XPath字符串。請參閱XPath非常簡單,此示例演示XPath字符串以表示從根元素(<configuration>)到<endpoint>元素(其中地址屬性駐留)的路徑。

1

幾個點這裏:

XML:

DocumentElement是在文檔中的頂級元素,所以在這種情況下是configuration。所以DocumentElement.Attributes是該頂級的屬性。

所以,如果你的XML是這樣的:

<configuration foo="bar"> 
... 
</configuration> 

然後DocumentElement.Attributes會發現foo屬性。

WCF綁定

我假設你想在這裏什麼是動態設置客戶端點的終點在所需的服務器。如果是這種情況,那麼這不是真正的辦法。

您最好通過在代碼中動態創建並連接到端點來完成此操作。

This answer涵蓋此相當不錯(在C#中,但原則是相同的)。

+0

:你給了一個很好的鏈接。謝謝 – Kenta

0

也許這將工作:

Dim XmlDoc As New XmlDocument() 
    Dim file As New StreamReader(filePath) 

    XmlDoc.Load(file) 

    For Each Element As XmlElement In XmlDoc.DocumentElement 
     If Element.Name = "system.service" Then 
      For Each Element2 As XmlElement In Element 
       If Element2.Name = "client" Then 
        For Each Element3 As XmlElement In Element2 
         For Each Node As XmlNode In Element3.ChildNodes 
          Node.Attributes(0).Value = YourNewAddress 
         Next 
        Next 
       End If 
      Next 
     End If 
    Next 

    file.Dispose() 
    file.Close() 

    Dim save As New StreamWriter(filePath) 

    XmlDoc.Save(save) 

    save.Dispose() 
    save.Close()