0

I have a .config file named app.exe.config , where i want to update a specific attribute value named address present inside endpoint tag .I tried the below code but could not able to get whats going wrong . I am new to vb.net .Please help .

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

I have used the below code but it fails

 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
Viku
  • 2,845
  • 4
  • 35
  • 63

3 Answers3

1

Couple of points here:

XML:

The DocumentElement is the top level element in the document, so in this case that is configuration. So DocumentElement.Attributes is attributes of that top level.

So if your xml looked like this:

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

Then DocumentElement.Attributes would find that foo attribute.

WCF Binding

I assume what you're trying here is to dynamically set the endpoint of a client to point at the required server. If that's the case then this isn't really the way to go about it.

You're better to do it by dynamically creating and connecting to the endpoint in code.

This answer covers this fairly well (in C#, but the principles are identical).

Community
  • 1
  • 1
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
1

You can use XPath syntax to get address attribute of endpoint element, for example :

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)

Notice that SelectSingleNode will get the first node matched XPath string supplied in the function parameter. See that XPath is intuitively easy, this example demonstrates XPath string to express path from root element (<configuration>) to <endpoint> element (where address attribute resides).

har07
  • 88,338
  • 12
  • 84
  • 137
0

Maybe This Will work:

    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()