更新xml文件中的属性值(.config文件)

时间:2014-02-20 09:29:51

标签: xml vb.net

我有一个名为app.exe.config的.config文件,其中我想更新端点标记内存在的名为address的特定属性值。我尝试了下面的代码,但无法得到什么错误。我是vb.net的新手。请帮助。

<?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

3 个答案:

答案 0 :(得分:1)

这里有几点意见:

<强> XML:

DocumentElement是文档中的顶级元素,因此在这种情况下为configuration。所以DocumentElement.Attributes是该顶级的属性。

所以如果你的xml看起来像这样:

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

然后DocumentElement.Attributes会找到foo属性。

WCF绑定

我假设您在这里尝试的是动态设置客户端的端点以指向所需的服务器。如果是这种情况,那么这不是真正的方法。

最好通过在代码中动态创建和连接端点来实现。

This answer相当不错(在C#中,但原则相同)。

答案 1 :(得分:1)

您可以使用XPath语法获取endpoint元素的地址属性,例如:

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>元素(地址属性所在的位置)的路径。

答案 2 :(得分: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()