0

我需要编辑一些 XML 文件,并想创建一个脚本来执行此操作。我几乎没有使用 XML 的经验,这就是我请求您帮助的原因。

下面的 XML 包含 2 个运输订单:一个具有正值元素,一个具有负值元素。我想做的是,对于每个包含负值的运输订单

  • pickup_task的addressid替换为delivery task的addressid,反之亦然
  • 用交付任务的值替换提货任务的任务窗口(from_instant 和 till_instant)。

是否可以制作一个 VB 脚本或批处理或其他东西来替换具有超过 200 个 transport_order 的文件?

谢谢

<?xml version="1.0" encoding="iso-8859-2"?>
<misu version="2010">
    <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>
            <id>NOCCO/12-007016_2</id>
            <order_number>NOCCO/12-007016_2</order_number>
            <order_date>2012-04-02</order_date>
            <contactId>C01137</contactId>
            <productId>0604-08L15000</productId>
            <sum>495.00</sum>
            <currency_code></currency_code>
            <reference>NOCCO/12-007016</reference>
            <amounts>
                <amount>
                    <unit_code>m2</unit_code>
                    <value>-5.00</value>
                </amount>
            </amounts>
            <pickup_task>
                <addressid>SFGH</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>C01137_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_orders>
</misu>
4

2 回答 2

0

来吧……这应该完成第 1 部分。我将把第 2 部分留给你(我不习惯 VB)。如您所见,我将 XML 文件命名为“data.xml”。

Sub Main()
    Dim xdoc As XDocument
    Dim filename As String
    filename = "../../data/data.xml"
    xdoc = XDocument.Load(filename)
    Dim xe As XElement
    xe = xdoc.Root.Element("transport_orders")
    For Each element In xe.Elements("transport_order")
        Dim val As Integer
        val = element.Element("amounts").Element("amount").Element("value").Value
        If (val < 0) Then
            Console.WriteLine(val)
            Dim puelement As XElement
            Dim delelement As XElement
            Dim putask As String
            puelement = element.Element("pickup_task").Element("addressid")
            delelement = element.Element("delivery_task").Element("addressid")
            putask = delelement.Value
            delelement.Value = puelement.Value
            puelement.Value = putask
        End If
    Next
    xdoc.Save(filename)
    Console.ReadLine()
End Sub
于 2012-04-21T07:57:13.127 回答
0

回到提要,事情运行得很好,因为我已经说过我的知识是有限的,我总是试图理解代码的作用并尝试重现以满足我的需求,所以这就是我为我的第二个问题所做的,我知道很蹩脚,但它可以完成工作,请确认

puelement2  = element.Element("pickup_task").Element("task_window").Element("from_instant")
delelement2 = element.Element("delivery_task").Element("task_window").Element("from_instant")
putask2 = delelement2.Value
delelement2.Value = puelement2.Value
puelement2.Value = putask2

puelement3 = element.Element("pickup_task").Element("task_window").Element("till_instant")
delelement3 = element.Element("delivery_task").Element("task_window").Element("till_instant")
putask3 = delelement3.Value
delelement3.Value = puelement3.Value
puelement3.Value = putask3  

谢谢。真的很有帮助。

于 2012-04-22T07:10:42.570 回答