0

Hello I have an Xelement which looks like this:

dim elementfromwebservice as xelement=

<companies>
<address idstring=xxxx hasorder=xxxx </address>
<address idstring=xxxx hasorder=xxxx </address>
<address idstring=xxxx hasorder=xxxx </address>
</companies> 

and I have which List which holds IDs here pseudosyntax:

dim listofid as (List of String) = new list(of string) 

I want update all address.@hasorders attributes of elementfromwebservice where idstring is in listofid

I hope you can help

Chers steven

SteveM
  • 13
  • 4

2 Answers2

1

Using VB.NET's support for XML, and a LINQ to XML query to find the elements you are interested in:

Dim elementFromWebService As XElement =
    <companies>
        <address idstring="xxxx" hasorder="xxxx"></address>
        <address idstring="xxxy" hasorder="xxxx"></address>
        <address idstring="xxxz" hasorder="xxxx"></address>
    </companies> 
Dim listOfId = { "xxxx", "xxxy" }
Dim addressesToUpdate =
    From e In elementFromWebService...<address>
    Where listOfId.Contains(e.@idstring)
For Each address In addressesToUpdate
    address.@hasorder = "updated"
Next

After this, elementFromWebService will contain:

<companies>
    <address idstring="xxxx" hasorder="updated"></address>
    <address idstring="xxxy" hasorder="updated"></address>
    <address idstring="xxxz" hasorder="xxxx"></address>
</companies>
Mark
  • 8,140
  • 1
  • 14
  • 29
  • Mark : This will not work because you are not updating values in original XElement, and you only have a portion of the original elements in your final results. – jdweng Jul 01 '15 at 19:48
  • @jdweng Not sure what you mean - the relevant `XElement`s in `elementFromWebService` are updated directly by this code. `addressesToUpdate` contains references to a subset of the original `XElement`s, and being references, the original values are being updated. The XML shown at the end of the answer is copied directly from `elementFromWebService.Dump()` in LINQPad, and shows that the result contains the original XML modified where the IDs matched. Make sense? You can copy the code into LINQPad to verify. Let me know if I am not understanding something. :-) – Mark Jul 01 '15 at 20:02
  • I s addressesToUpdate a copy of the original XElement or is it linked to the original XElement? – jdweng Jul 01 '15 at 21:46
  • @jdweng `addressesToUpdate` is an `IEnumerable(Of XElement)` of references to the original `XElements` that match the query. – Mark Jul 01 '15 at 22:03
  • I will check this, when I'm at work. Thanks for the answers – SteveM Jul 02 '15 at 05:15
0

Try this

Sub Main()
        Dim input As String = _
        "<companies>" & _
        "<address idstring=""xxxx"" hasorder=""xxxx"" />" & _
        "<address idstring=""xxxx"" hasorder=""xxxx"" />" & _
        "<address idstring=""xxxx"" hasorder=""xxxx"" />" & _
        "</companies>"

        Dim addresses As XElement = XElement.Parse(input)
        Dim listofid As List(Of String) = New List(Of String)
        listofid.Add("xxxx")

        For Each address In addresses.Descendants("address")
            Dim idstring = address.Attribute("idstring")
            If listofid.Contains(idstring) Then
                address.Attribute("hasorder").Value = "new value"
            End If
        Next address

    End Sub
jdweng
  • 33,250
  • 2
  • 15
  • 20