0

2 question pumped up after trying to edit an xml file in vb, i try-ed to find answer around but those did not worked as expected, what i'm trying to achieve is that for those transport_orders that have negative , replace the negative value with its positive.Keep in mind that the values is a number with 2 decimals and it's positive shouls remain in the same format.

Source xml looks like this:

<transport_orders>
<transport_order>
<id>NOCCO/12-006798_1</id>
<order_number>NOCCO/12-006798_1</order_number>
<order_date>2012-03-30</order_date>
<contactId>C04396</contactId>
<productId>0103-01101025</productId>
<sum>3135.51</sum>
<currency_code></currency_code>
<reference>NOCCO/12-006798</reference>
<amounts>
<amount>
<unit_code>kg</unit_code>
<value>-324.00</value>
</amount>
</amounts>
<pickup_task>
<addressid>BUCU</addressid>
<task_window>
<from_instant>2012-04-20T18:26:43</from_instant>
<till_instant>2012-04-20T18:26:43</till_instant>
</task_window>
</pickup_task>
<delivery_task>
<addressid>C04396_1</addressid>
<task_window>
<from_instant>2012-04-23T00:00:00</from_instant>
<till_instant>2012-04-24T00:00:00</till_instant>
</task_window>
</delivery_task>
</transport_order>
<transport_order>
...
  1. What I have tryed is the code underneath, but i think i'm missing something because the value is a number with 2 decimals and the only result i got is instead 324 i get 32400

    element.Element("amounts").Element("amount").Element("value").decimal("n2") = -element.Element("amounts").Element("amount").Element("value").Value

  2. How can i replace from 2012-04-20T18:26:43 only the date or only the hors, for example i would like to edit the pickup task's-time window-from_instant date with the date of the dilivery's task-time windows-till instant, but keep the time(hour,min,sec) intact.

Thanks.

1 Answer 1

1
    Dim doc = <transport_orders>
                  <transport_order>
                      (...)
                  </transport_order>
              </transport_orders>

    Dim culture = New CultureInfo("en")

    Dim negativeAmmounts = (From item In doc.Descendants("value")
                           Let value = Double.Parse(item.Value, culture)
                           Where value < 0
                           Select New With {
                               .Item = item,
                               .Value = value
                           }).ToList()

    negativeAmmounts.ForEach(Sub(e) e.Item.Value = -1 * e.Value)

After that you can check if all values were replaced, for example by printing the whole document into a string:

    Dim outerXml = doc.ToString()
Sign up to request clarification or add additional context in comments.

5 Comments

Error 1 Lambda parameter 'e' hides a variable in an enclosing block, a previously defined range variable, or an implicitly declared variable in a query expression. Can i use doc as doc As XDocument? and of course doc = XDocument.Load(filename)
That's because you have method parameter named e. Replace it with something else on that ForEach statement, like: negativeAmmounts.ForEach(Sub(item) item.Item.Value = -1 * item.Value). About XDocument - doc is XDocument object :) So you can use XDocument.Load() to create it as well.
Thanks, work partially, <value>-324.00</value> is now <value>324</value>, but i need the decimals, how shall I keep the decimals?
Use .ToString() method to get the value in proper format: negativeAmmounts.ForEach(Sub(item) item.Item.Value = (-1 * item.Value).ToString("0.00"))
gorgeous, this was realy helpfull. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.