2011-11-22 92 views
3

好吧,这把我现在占领了几个小时,我仍然有它没有解释: 我的XML开始是这样的:XmlNodeList保持为空 - 为什么是这样?

<?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"/> 
... 

现在我想要做的是与AllowedInSlots工作,但当创建一个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); 

它没有工作,虽然如此,事情必须有错。

第二编辑: 包括路径的每个部分的前缀做了窍门。

回答

3

确实是命名空间管理器。

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

意味着一切都在该命名空间(除非其他默认声明或元素声明自己的命名空间)。您将需要使用名称空间管理器在该名称空间中进行搜索。

+0

好的,编辑了这个问题,以包含我尝试处理该问题的(错误的?)代码。 – Stefan

+0

再次编辑(终于搞定了)。感谢您帮助我,如果您没有告诉我名称空间实际上是问题,那么可能会先尝试其他方法。 – Stefan

+0

你在问题的第二个问题中写了答案,但我会在这里重写它,以供将来看到的任何人使用。您必须在每个零件上指定名称空间:“/ iso:ISO15745Profile/iso:ProfileBody/iso:ApplicationProcess ....” –