0

我有一个我试图阅读的 XML 文档,但我的查询一直没有返回任何结果。我使用的查询是...

Dim products = From product In doc.Descendants("DeviceInstance") _
    Select product.Attribute("ProductRefId").Value

For Each result In products
    MessageBox.Show(result.ToString())
Next

我哪里错了??

xml见下文

<?xml version="1.0"?>
<KNX xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://knx.org/xml/project/10" CreatedBy="knxconv" ToolVersion="4.0.1004.62808">
  <Project Id="P-0225">
    <Installations>
      <Installation Name="" InstallationId="0">
        <Topology>
          <Area Id="P-0225-0_A-734" Name="East" Address="1" Description="">
            <Line Id="P-0225-0_L-736" Name="Ground" Address="1" MediumTypeRefId="MT-0" Comment="" Description="">
              <DeviceInstance Name="" Id="P-0225-0_DI-761" ProductRefId="M-0064_H-MTN647893-1-O000C_P-MTN647893" Hardware2ProgramRefId="M-0064_H-MTN647893-1-O000C_HP-480B-21-4CB2-O000C" Address="5" Comment="" LastModified="2012-07-04T19:27:44" LastDownload="2012-08-22T11:27:40" InstallationHints="" IndividualAddressLoaded="true" ApplicationProgramLoaded="true" ParametersLoaded="true" CommunicationPartLoaded="true" MediumConfigLoaded="true" Description="SWA1101" IsCommunicationObjectVisibilityCalculated="true">
                <ParameterInstanceRefs>
                  <ParameterInstanceRef RefId="M-0064_A-480B-21-4CB2-O000C_P-1_R-1" Value="0"/>
                  <ParameterInstanceRef RefId="M-0064_A-480B-21-4CB2-O000C_P-2834_R-2834" Value="0"/>
                  <ParameterInstanceRef RefId="M-0064_A-480B-21-4CB2-O000C_P-2835_R-2835" Value="0"/>
                </ParameterInstanceRefs>
                <ComObjectInstanceRefs>
                  <ComObjectInstanceRef RefId="M-0064_A-480B-21-4CB2-O000C_O-31_R-243" IsActive="true">
                  </ComObjectInstanceRef>
                  <Connectors>
                    <Send GroupAddressRefId="P-0225-0_GA-128"/>
                  </Connectors>
                </ComObjectInstanceRefs>
              </DeviceInstance>
            </Line>
          </Area>
        </Topology>
      </Installation>
    </Installations>
  </Project>
</KNX>
4

2 回答 2

1

Almost equivalent to @Sean Bright's answer (except slightly less general):

Imports <xmlns="http://knx.org/xml/project/10">

...

Dim products = doc...<DeviceInstance>.Select( _
                   Function(product) product.@ProductRefId)

The LINQ only solution is also possible (effectively combining my solution with Sean's):

Imports <xmlns="http://knx.org/xml/project/10">

...

Dim products = From product In doc...<DeviceInstance> _
                   Select product.@ProductRefId

(This is untested.)

于 2012-09-26T01:42:54.930 回答
1

尝试这个:

Dim ns = doc.Root.GetDefaultNamespace()

Dim products = From product In doc.Descendants(ns + "DeviceInstance") _
    Select product.Attribute("ProductRefId").Value
于 2012-09-25T14:11:28.803 回答