xmlvb.netxml-literals

XML Literals with Loops and conditionals


I am trying to construct a relatively simple chunk of XML to send to a web service. I am using XML Literals, and the below currently works fine:

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>

However, I want to add in a condition, which will only include the <LIN> element if a certain condition is true. So, something like this:

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>

But I cannot seem to get the syntax correct.

Can anyone help?


Solution

  • Move the Where to the From

    e.g.

    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>
    

    Your code would look like

    <%= 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>
    %>