使用 LINQ 查询 XDocument to Return 列表

Query XDocument with LINQ to Return List

我有一个 xml 字符串(为了便于测试缩短为只有 2 行):

<?xml version="1.0" encoding="utf-8"?>
<DataTable Uid="DT_RetRes">
  <Rows>
    <Row>
      <Cells>
        <Cell>
          <ColumnUid>AVAIL</ColumnUid>
          <Value>N</Value>
        </Cell>
        <Cell>
          <ColumnUid>ItemCode</ColumnUid>
          <Value>PRODUCTID123#</Value>
        </Cell>
        <Cell>
          <ColumnUid>Dscription</ColumnUid>
          <Value>Basic Product 1 with hash</Value>
        </Cell>
        <Cell>
          <ColumnUid>U_JOW_CMNT</ColumnUid>
          <Value></Value>
        </Cell>
      </Cells>
    </Row>
    <Row>
      <Cells>
        <Cell>
          <ColumnUid>AVAIL</ColumnUid>
          <Value>Y</Value>
        </Cell>
        <Cell>
          <ColumnUid>ItemCode</ColumnUid>
          <Value>PRODUCTID234</Value>
        </Cell>
        <Cell>
          <ColumnUid>Dscription</ColumnUid>
          <Value>Basic Product 2 no hash</Value>
        </Cell>
        <Cell>
          <ColumnUid>U_JOW_CMNT</ColumnUid>
          <Value></Value>
        </Cell>
      </Cells>
    </Row>
</Rows>
</DataTable>

我正在尝试查询 Xdocument return 所有 ItemCode 值的列表,其中值 AVAIlY

我的代码目前如下:

Dim xdoc As XDocument = XDocument.Parse(xmlString)

Dim result = xdoc.Elements("Rows").
Where(Function(x) x.Elements("ColumnUid").Value = "AVAIL" And 
x.Elements("Value").Value = "Y").
Select(Function(x) x.<ItemCode>.Value).ToList

        MessageBox.Show(result.Count)

然而,这似乎从来没有 return 任何东西,我已经尝试了查询的 Where 和 Select 部分的许多变体,但没有成功。我是 LINQ 的新手,可以猜测查询构造不正确,可能没有以正确的方式访问正确的元素。

任何帮助将不胜感激。

我想到了这个:

Dim isAvail = xDoc.<DataTable>.<Rows>.<Row>.<Cells>.
              Where(Function(x) x.<Cell>.<ColumnUid>.Value = "AVAIL" AndAlso
                                x.<Cell>.<Value>.Value = "Y")


Dim availItemCodes = isAvail.<Cell>.
                     Where(Function(y) y.<ColumnUid>.Value = "ItemCode").<Value>.
                     Select(Function(v) v.Value)

isAvail<Cells> 的列表(嗯,IEnumerable),其中包含所需的 <Cell>

您可以看到如何将其分解为更小的语句使其更易于使用。

一种一体化的方法,诚然是一个更复杂的查询, 首先是 XML

    Dim someXML As XElement
    'to load from file
    ' someXML=XElement.Load("path here")
    'OR parse string
    ' someXML = XElement.Parse("string here")
    'OR for testing use a literal

    someXML = <DataTable Uid="DT_RetRes">
                  <Rows>
                      <Row>
                          <Cells>
                              <Cell>
                                  <ColumnUid>AVAIL</ColumnUid>
                                  <Value>N</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>ItemCode</ColumnUid>
                                  <Value>PRODUCTID123#</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>Dscription</ColumnUid>
                                  <Value>Basic Product 1 with hash</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>U_JOW_CMNT</ColumnUid>
                                  <Value></Value>
                              </Cell>
                          </Cells>
                      </Row>
                      <Row>
                          <Cells>
                              <Cell>
                                  <ColumnUid>AVAIL</ColumnUid>
                                  <Value>Y</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>ItemCode</ColumnUid>
                                  <Value>PRODUCTID234</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>Dscription</ColumnUid>
                                  <Value>Basic Product 2 no hash</Value>
                              </Cell>
                              <Cell>
                                  <ColumnUid>U_JOW_CMNT</ColumnUid>
                                  <Value></Value>
                              </Cell>
                          </Cells>
                      </Row>
                  </Rows>
              </DataTable>

然后是代码

    Dim ie As IEnumerable(Of String)
    ie = From el In someXML.<Rows>.<Row>.<Cells>.<Cell>
            Where el.<ColumnUid>.Value = "AVAIL" AndAlso el.<Value>.Value = "Y"
             From item In el.Parent.<Cell>
                Where item.<ColumnUid>.Value = "ItemCode"
                Select item.<Value>.Value