XmlNodeList仍然为空——为什么会这样?

本文关键字:为什么 XmlNodeList | 更新日期: 2023-09-27 18:15:23

好吧,这让我忙了好几个小时,但我仍然没有解释:我的XML开头是这样的:

<?xml version="1.0" encoding="iso-8859-1"?>
<ISO15745Profile xmlns="http://www.profibus.com/GSDML/2003/11/DeviceProfile" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.profibus.com/GSDML/2003/11/DeviceProfile ..'XSD'GSDML-DeviceProfile-v2.1.xsd">
<ProfileHeader>
    <ProfileIdentification>PROFINET Device Profile</ProfileIdentification>
    <ProfileRevision>1.00</ProfileRevision>
    <ProfileName>Device Profile for PROFINET Devices</ProfileName>
    <ProfileSource>PROFIBUS Nutzerorganisation e. V. (PNO)</ProfileSource>
    <ProfileClassID>Device</ProfileClassID>
    <ISO15745Reference>
        <ISO15745Part>4</ISO15745Part>
        <ISO15745Edition>1</ISO15745Edition>
        <ProfileTechnology>GSDML</ProfileTechnology>
    </ISO15745Reference>
</ProfileHeader>
<ProfileBody>
    <DeviceIdentity DeviceID="0x000A" VendorID="0x00B0">
        <InfoText TextId="InfoTextId1"/>
        <VendorName Value="Phoenix Contact GmbH"/>
    </DeviceIdentity>
    <DeviceFunction>
        <Family MainFamily="I/O" ProductFamily="Inline"/>
    </DeviceFunction>
    <ApplicationProcess>
        <DeviceAccessPointList>
            <DeviceAccessPointItem ID="DIM 1" FixedInSlots="0" PhysicalSlots="0..64" MinDeviceInterval="32" ModuleIdentNumber="0x00000300" DNS_CompatibleName="IL-PN-BK-2TX" ImplementationType="ERTEC200" ObjectUUID_LocalIndex="1">
                <ModuleInfo>
                    <Name TextId="IL PN BK DI8 DO4 2TX"/>
                    <InfoText TextId="InfoTextId1"/>
                    <VendorName Value="Phoenix Contact"/>
                    <OrderNumber Value="2703994"/>
                </ModuleInfo>
                <SubslotList>
                    <SubslotItem SubslotNumber="32768" TextId="SubSlot_Interface"/>
                    <SubslotItem SubslotNumber="32769" TextId="SubSlot_Port1"/>
                    <SubslotItem SubslotNumber="32770" TextId="SubSlot_Port2"/>
                </SubslotList>
                <IOConfigData MaxInputLength="512" MaxOutputLength="512"/>
                <UseableModules>
                    <ModuleItemRef FixedInSlots="1" ModuleItemTarget="1"/>
                    <ModuleItemRef AllowedInSlots="4..64" ModuleItemTarget="2"/>
                    <ModuleItemRef AllowedInSlots="4..64" ModuleItemTarget="3"/>
                    <ModuleItemRef AllowedInSlots="4..64" ModuleItemTarget="4"/>
                    <ModuleItemRef AllowedInSlots="4..64" ModuleItemTarget="5"/>
                    <ModuleItemRef AllowedInSlots="4..64" ModuleItemTarget="6"/>
...

现在我要做的是与alloweddinslots一起工作,但是当使用

创建XmlNodeList时
XmlDocument gsdml = new XmlDocument();
gsdml.Load(fpfad);
XmlNodeList ModuleItemRef = gsdml.SelectNodes("/ISO15745Profile/ProfileBody/ApplicationProcess/DeviceAccessPointList/DeviceAccessPointItem/UseableModules");

XmlNodeList仍然为空。我做错了什么?我想也许我必须与Namespacemanager合作并尝试过,但这没有任何作用。

这是我之前尝试过的:

XmlDocument gsdml = new XmlDocument();
gsdml.Load(fpfad);
XmlNamespaceManager mgr = new XmlNamespaceManager(gsdml.NameTable);
mgr.AddNamespace("iso", "http://www.profibus.com/GSDML/2003/11/DeviceProfile");
XmlNodeList ModuleItemRef = gsdml.SelectNodes("/iso:ISO15745Profile/ProfileBody/ApplicationProcess/DeviceAccessPointList/DeviceAccessPointItem/UseableModules", mgr);

但是它没有工作,所以一定有什么地方出错了。

编辑2:在路径的每个部分中加入前缀就成功了。

XmlNodeList仍然为空——为什么会这样?

它确实是名称空间管理器。

xmlns="http://www.profibus.com/GSDML/2003/11/DeviceProfile"

表示所有内容都在该名称空间中(除非声明了另一个默认值,或者元素声明了自己的名称空间)。您将需要使用名称空间管理器在该名称空间中进行搜索。