具有循环和条件的XML文字

时间:2018-10-26 15:06:33

标签: xml vb.net xml-literals

我正在尝试构造一个相对简单的XML块以发送到Web服务。我正在使用XML文字,并且以下内容目前可以正常运行:

Dim OrderXML As XElement =
    <PARAM>
        <FLD NAME="CUSORDREF"><%= CustomerOrderNo %></FLD>
        <FLD NAME="BPCORD"><%= CustomerCode %></FLD>
        <GRP ID="SOH1_5">
            <FLD NAME="YHOLDSTA">3</FLD>
        </GRP>
        <TAB ID="SOH4_1">
            <%= From l In Me.OrderLines
                Select
                    <LIN NUM=<%= l.Key %>>
                        <FLD NAME="ITMREF"><%= l.Value.Sku %></FLD>
                        <FLD NAME="QTY"><%= l.Value.ActualQuantity %></FLD>
                    </LIN>
            %>
        </TAB>
        <GRP ID="ADB2_1">
            <LST NAME="BPRNAM" SIZE="2" TYPE="Char">
                <ITM><%= CustomerName %></ITM>
            </LST>
            <FLD NAME="CRY">GB</FLD>
            <LST NAME="BPAADDLIG" SIZE="3" TYPE="Char">
                <ITM><%= DeliveryAddress1 %></ITM>
                <ITM><%= DeliveryAddress2 %></ITM>
                <ITM><%= DeliveryAddress3 %></ITM>
            </LST>
            <FLD NAME="POSCOD"><%= DeliveryPostcode %></FLD>
            <FLD NAME="CTY"><%= DeliveryCity %></FLD>
            <FLD NAME="SAT"><%= DeliveryRegion %></FLD>
        </GRP>
    </PARAM>

但是,我要添加一个条件,如果某个条件为true,则仅包含<LIN>元素。所以,像这样:

Dim OrderXML As XElement =
    <PARAM>
        <FLD NAME="CUSORDREF"><%= CustomerOrderNo %></FLD>
        <FLD NAME="BPCORD"><%= CustomerCode %></FLD>
        <GRP ID="SOH1_5">
            <FLD NAME="YHOLDSTA">3</FLD>
        </GRP>
        <TAB ID="SOH4_1">
            <%= From l In Me.OrderLines
                Select
                    <LIN NUM=<%= l.Key %>>
                        <FLD NAME="ITMREF"><%= l.Value.Sku %></FLD>
                        <FLD NAME="QTY"><%= l.Value.ActualQuantity %></FLD>
                    </LIN>
                Where l.Value.ImportLine = True
            %>
        </TAB>
        <GRP ID="ADB2_1">
            <LST NAME="BPRNAM" SIZE="2" TYPE="Char">
                <ITM><%= CustomerName %></ITM>
            </LST>
            <FLD NAME="CRY">GB</FLD>
            <LST NAME="BPAADDLIG" SIZE="3" TYPE="Char">
                <ITM><%= DeliveryAddress1 %></ITM>
                <ITM><%= DeliveryAddress2 %></ITM>
                <ITM><%= DeliveryAddress3 %></ITM>
            </LST>
            <FLD NAME="POSCOD"><%= DeliveryPostcode %></FLD>
            <FLD NAME="CTY"><%= DeliveryCity %></FLD>
            <FLD NAME="SAT"><%= DeliveryRegion %></FLD>
        </GRP>
    </PARAM>

但是我似乎无法正确理解语法。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

从哪里移动到哪里

例如

Dim nums() As Integer = {1, 2, 3, 4, 5}
Dim xe As XElement
xe =  <root>
        <tab>
            <%= From i In nums Where i Mod 2 = 1
                Select <lin><%= i.ToString %></lin>
            %>
        </tab>
      </root>

您的代码看起来像

<%= From l In Me.OrderLines Where l.Value.ImportLine = True
    Select
        <LIN NUM=<%= l.Key %>>
            <FLD NAME="ITMREF"><%= l.Value.Sku %></FLD>
            <FLD NAME="QTY"><%= l.Value.ActualQuantity %></FLD>
        </LIN>
%>