0

我正在尝试构建一个相对简单的 XML 块以发送到 Web 服务。我正在使用 XML Literals,以下当前工作正常:

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>

但是,我想添加一个条件,该条件仅在<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>

但我似乎无法使语法正确。

任何人都可以帮忙吗?

4

1 回答 1

0

将 Where 移至 From

例如

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>
%>
于 2018-10-26T15:31:39.570 回答