日期:2014-05-19  浏览次数:20903 次

(修正)使用XPATH解析xml的问题
xml如下:
<?xml   version   =   '1.0 '?>
<Reservation   xmlns= "reservation.fidelio.1.0 "   mfShareAction= "NA "   mfReservationAction= "ADD ">
    <HotelReference>
        <hotelCode> FOXH </hotelCode>
    </HotelReference>
    <ResProfiles>
        <ResProfile>
            <Profile   xmlns= "profile.fidelio.1.2 "   profileType= "D "   gender= "UNKNOWN ">
                <profileID> 49717 </profileID>
            </Profile>
        </ResProfile>
    </ResProfiles>
</Reservation>

C#代码如下:
XmlDocument   doc   =   new   XmlDocument();
                                doc.Load(filename);

                                XmlNamespaceManager   nsmgr   =   new   XmlNamespaceManager(doc.NameTable);
                                nsmgr.AddNamespace( "res ", "reservation.fidelio.1.0 ");  
                                nsmgr.AddNamespace( "pro ", "profile.fidelio.1.2 ");


                                string   d   =   doc.SelectSingleNode( "/res:Reservation/res:HotelReference/res:hotelCode ").InnerText;

                                string   a   =   doc.SelectSingleNode( "/res:Reservation/res:ResProfiles/res:ResProfile/pro:Profile/pro:ProfileID ",   nsmgr).InnerText;

为何d可以读出FOXH,但是a却出错,请高手指教


------解决方案--------------------
不是ProfileID
是profileID