Reputation: 43
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>
...
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
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.
Upvotes: 0
Views: 600
Reputation: 125630
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()
Upvotes: 1