简体   繁体   English

在VB.net中进行XML编辑

[英]XML edit in VB.net

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. 在尝试在vb中编辑xml文件后出现了2个问题,我尝试查找答案,但是这些未按预期工作,我想要实现的是对于那些具有负数的transport_order,替换负值请记住,值是一个带有两位小数的数字,它的正数应该保持相同的格式。

Source xml looks like this: 源xml如下所示:

<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 我尝试过的是下面的代码,但是我想我丢失了一些东西,因为该值是带有2个小数的数字,而我得到的唯一结果却是324我得到了32400

    element.Element("amounts").Element("amount").Element("value").decimal("n2") = -element.Element("amounts").Element("amount").Element("value").Value element.Element(“ amounts”)。Element(“ amount”)。Element(“ value”)。decimal(“ n2”)= -element.Element(“ amounts”)。Element(“ amount”)。Element(“价值”)。价值

  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. 我该如何从2012-04-20T18:26:43仅替换日期或日期,例如,我想用提货任务时间窗的日期编辑提货任务的时间窗-from_instant日期即时,但保持时间(小时,分钟,秒)不变。

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM