Jeremy
Jeremy

Reputation: 87

Increment element by one

Using VB.NET & LINQ; I am writing to an XML file that I need to update the "click count". I am trying to create a function that will increment the element appClick by 1 each time someone clicks the link.

<applications>
  <app id="1">
    <appName>Service Desk</appName>
    <appLink>https://websiteurlhere.com</appLink>
    <appFav>1</appFav>
    <appClick>0</appClick>
  </app>
</applications>

So I have the subroutine that calls the applications and it is working perfectly. But is lacks the ability to increment the appClick element.

Public Sub appCall(ByVal x As String) ' x = application name
    Dim appQuery = _
        From c In doc.<applications>.<app> _
        Where c.<appName>.Value = x _
        Select c.<appLink>

    For Each result In appQuery
        System.Diagnostics.Process.Start(result.Value)
    Next
End Sub

If the code can be improved, I am also open to suggestions on that.

Upvotes: 2

Views: 145

Answers (1)

Victor Zakharov
Victor Zakharov

Reputation: 26424

Instead of just Select c.<appLink>, do a Select c.<appClick>, c.<appLink>.

Then you can do:

For Each result In appQuery
    result.appClick.Value += 1
    System.Diagnostics.Process.Start(result.appLink.Value)
Next

And don't forget to save your document after that.

doc.Save("fileName")

Here is a sample I used for testing:

Public Sub appCall(ByVal x As String) ' x = application name
  Dim doc = <applications>
              <app id="1">
                <appName>Service Desk</appName>
                <appLink>https://websiteurlhere.com</appLink>
                <appFav>1</appFav>
                <appClick>0</appClick>
              </app>
            </applications>

  Dim appQuery = _
      From c In doc.<app> _
      Where c.<appName>.Value = x _
      Select c.<appClick>, c.<appLink>

  For Each result In appQuery
    result.appClick.Value += 1
    Debug.WriteLine(result.appLink.Value)
  Next
End Sub

Upvotes: 1

Related Questions