2010-08-12 78 views
1

試圖找出XPath併成功創建了一個XPathExpression,可以讓我抓取事件,但是任何嵌套的子組似乎都無法查詢,我可以獲取所有子節點在「事件」組上,但是我無法訪問的「位置名稱」字段。無法使用XPath表達式訪問子節點

我試圖找出如何讓LOCATIONNAME孩子到我的查詢

這裏是我的表達:

Dim nav As XPathNavigator 
Dim docNav As XPathDocument 
docNav = New XPathDocument(New XmlNodeReader(rssDoc.DocumentElement())) 

nav = docNav.CreateNavigator 
'Initial XPathNavigator to start at the root.' 
nav.MoveToRoot() 

'Sort the Xpath query - this might be black magic' 
Dim expr As XPathExpression = nav.Compile("/CALENDAR_ENTERPRISE/EVENTS") 
expr.AddSort("StartDate", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text) 

Dim theEvents As XPathNodeIterator = nav.Select(expr) 
While (theEvents.MoveNext()) 
    Dim iStartDate As XPathNodeIterator = theEvents.Current.SelectChildren("StartDate", "") 
    Dim iStartTime As XPathNodeIterator = theEvents.Current.SelectChildren("StartTime", "") 
End While 

這裏是我的xml:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<CALENDAR_ENTERPRISE Created="8/12/2010 10:44:50 AM"> 
    <EVENT type="group"> 
    <Name type="text">Fridays After Dark Presents: CHAT and FADD's Halloween Dance</Name> 
    <Description type="text">&amp;#160;Come decked out in your favorite Halloween costume and dance the night away!</Description> 
    <ContactName type="text" /> 
    <ContactPhone type="text" /> 
    <ContactEmail type="text" /> 
    <Department type="text">SUS</Department> 
    <Categorization type="text">Undergraduate::Student Life</Categorization> 
    <PrivateFlag type="text">N</PrivateFlag> 
    <Highlight type="text">N</Highlight> 
    <Locations type="group"> 
     <Location type="group"> 
     <LocationName type="text">Griffin's Den</LocationName> 
     <SetupNotes type="text" /> 
     </Location> 
    </Locations> 
    <Resources type="group" /> 
    <County type="text" /> 
    <Country type="text">United States</Country> 
    <RegistrationEnabled type="text" /> 
    <RegistrationMaxReg type="text" /> 
    <RegistrationDisplaySpaceAvail type="text" /> 
    <RegistrationType type="text" /> 
    <RegistrationTemplate type="text" /> 
    <InternalCustom1 type="text" /> 
    <InternalCustom2 type="text" /> 
    <InternalCustom3 type="text" /> 
    <InternalCustom4 type="text" /> 
    <InformationStatus type="text" /> 
    <InternalComments type="text" /> 
    <ExternalField1 type="text" /> 
    <ExternalField2 type="text" /> 
    <ExternalField3 type="text" /> 
    <ExternalField4 type="text" /> 
    <AllDay type="text">N</AllDay> 
    <StartDate type="text">10/29/2010</StartDate> 
    <StartTime type="text">10:00 PM</StartTime> 
    <EndDate type="text">10/30/2010</EndDate> 
    <EndTime type="text">1:00 AM</EndTime> 
    <RecurType type="text">One Time</RecurType> 
    <RecurDays type="text">1</RecurDays> 
    <RecurEndDate type="text">10/29/2010</RecurEndDate> 
    <Address1 type="text" /> 
    <Address2 type="text" /> 
    <City type="text" /> 
    <State type="text" /> 
    <Zipcode type="text" /> 
    <Phone type="text" /> 
    <InternetAddress type="text" /> 
    <ImportSeriesId type="text">232</ImportSeriesId> 
    <ImportOccurrenceId type="text">511</ImportOccurrenceId> 
    <CreatedOn type="text">7/9/2010 9:02:30 AM</CreatedOn> 
    <ModifiedOn type="text">8/9/2010 2:27:00 PM</ModifiedOn> 
    <AttachmentBinary type="group" /> 
    <ImageBinary type="group" /> 
    <ExternalSeriesId type="text" /> 
    <ExternalOccurrenceId type="text" /> 
    <EventOwnerName type="text">Murphy, Krista</EventOwnerName> 
    </EVENT> 
    <EVENT type="group"> 
    <Name type="text">Undergraduate Saturday Visit</Name> 
    <Description type="text">Prospective students are welcomed to view a short presentation, speak with Admissions Counselors, tour the campus, and meet with representatives from Financial Aid. 
++++ 
&lt;br /&gt;&lt;div&gt;More info available here: 
&lt;br /&gt;&lt;a href="http://www.chc.edu/Visit_and_Information_Sessions"&gt;http://www.chc.edu/Visit_and_Information_Sessions&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;</Description> 
    <ContactName type="text">Undergraduate Admissions</ContactName> 
    <ContactPhone type="text">215-248-7001</ContactPhone> 
    <ContactEmail type="text" /> 
    <Department type="text">SUS</Department> 
    <Categorization type="text">Public Events||Undergraduate::Admissions</Categorization> 
    <PrivateFlag type="text">N</PrivateFlag> 
    <Highlight type="text">N</Highlight> 
    <Locations type="group" /> 
    <Resources type="group" /> 
    <County type="text" /> 
    <Country type="text">United States</Country> 
    <RegistrationEnabled type="text" /> 
    <RegistrationMaxReg type="text" /> 
    <RegistrationDisplaySpaceAvail type="text" /> 
    <RegistrationType type="text" /> 
    <RegistrationTemplate type="text" /> 
    <InternalCustom1 type="text" /> 
    <InternalCustom2 type="text" /> 
    <InternalCustom3 type="text" /> 
    <InternalCustom4 type="text" /> 
    <InformationStatus type="text" /> 
    <InternalComments type="text" /> 
    <ExternalField1 type="text" /> 
    <ExternalField2 type="text" /> 
    <ExternalField3 type="text" /> 
    <ExternalField4 type="text" /> 
    <AllDay type="text">N</AllDay> 
    <StartDate type="text">10/30/2010</StartDate> 
    <StartTime type="text" /> 
    <EndDate type="text">10/30/2010</EndDate> 
    <EndTime type="text" /> 
    <RecurType type="text">One Time</RecurType> 
    <RecurDays type="text">1</RecurDays> 
    <RecurEndDate type="text">10/30/2010</RecurEndDate> 
    <Address1 type="text" /> 
    <Address2 type="text" /> 
    <City type="text" /> 
    <State type="text" /> 
    <Zipcode type="text" /> 
    <Phone type="text" /> 
    <InternetAddress type="text" /> 
    <ImportSeriesId type="text">255</ImportSeriesId> 
    <ImportOccurrenceId type="text">559</ImportOccurrenceId> 
    <CreatedOn type="text">8/4/2010 9:57:25 AM</CreatedOn> 
    <ModifiedOn type="text">8/4/2010 2:09:25 PM</ModifiedOn> 
    <AttachmentBinary type="group" /> 
    <ImageBinary type="group" /> 
    <ExternalSeriesId type="text" /> 
    <ExternalOccurrenceId type="text" /> 
    <EventOwnerName type="text">Boccuzzi, Lori</EventOwnerName> 
    </EVENT> 
    <EVENT type="group"> 
    <Name type="text">Shriner's Hospital Service</Name> 
    <Description type="text">&lt;div&gt;Visit the Campus Ministry Office for more information.&lt;/div&gt;</Description> 
    <ContactName type="text" /> 
    <ContactPhone type="text" /> 
    <ContactEmail type="text" /> 
    <Department type="text">SUS</Department> 
    <Categorization type="text">Undergraduate::Student Life</Categorization> 
    <PrivateFlag type="text">N</PrivateFlag> 
    <Highlight type="text">N</Highlight> 
    <Locations type="group"> 
     <Location type="group"> 
     <LocationName type="text">Off-campus</LocationName> 
     <SetupNotes type="text" /> 
     </Location> 
    </Locations> 
    <Resources type="group" /> 
    <County type="text" /> 
    <Country type="text">United States</Country> 
    <RegistrationEnabled type="text" /> 
    <RegistrationMaxReg type="text" /> 
    <RegistrationDisplaySpaceAvail type="text" /> 
    <RegistrationType type="text" /> 
    <RegistrationTemplate type="text" /> 
    <InternalCustom1 type="text" /> 
    <InternalCustom2 type="text" /> 
    <InternalCustom3 type="text" /> 
    <InternalCustom4 type="text" /> 
    <InformationStatus type="text" /> 
    <InternalComments type="text" /> 
    <ExternalField1 type="text" /> 
    <ExternalField2 type="text" /> 
    <ExternalField3 type="text" /> 
    <ExternalField4 type="text" /> 
    <AllDay type="text">N</AllDay> 
    <StartDate type="text">10/30/2010</StartDate> 
    <StartTime type="text">12:30 PM</StartTime> 
    <EndDate type="text">10/30/2010</EndDate> 
    <EndTime type="text">3:30 PM</EndTime> 
    <RecurType type="text">One Time</RecurType> 
    <RecurDays type="text">1</RecurDays> 
    <RecurEndDate type="text">10/30/2010</RecurEndDate> 
    <Address1 type="text" /> 
    <Address2 type="text" /> 
    <City type="text" /> 
    <State type="text" /> 
    <Zipcode type="text" /> 
    <Phone type="text" /> 
    <InternetAddress type="text" /> 
    <ImportSeriesId type="text">293</ImportSeriesId> 
    <ImportOccurrenceId type="text">643</ImportOccurrenceId> 
    <CreatedOn type="text">8/12/2010 10:16:07 AM</CreatedOn> 
    <ModifiedOn type="text">8/12/2010 10:16:07 AM</ModifiedOn> 
    <AttachmentBinary type="group" /> 
    <ImageBinary type="group" /> 
    <ExternalSeriesId type="text" /> 
    <ExternalOccurrenceId type="text" /> 
    <EventOwnerName type="text">Murphy, Krista</EventOwnerName> 
    </EVENT> 
</CALENDAR_ENTERPRISE> 
+0

那麼你的問題是什麼?毫無疑問。另外,提供的XML文檔沒有任何名爲'CALENDAR_ENTERPRISE'或'EVENTS'的元素。所以,你似乎沒有提供正確的XML文檔。 請提供正確的XML文檔並定義您想要選擇哪個節點。 – 2010-08-13 01:36:51

回答

0

嘗試

/CALENDAR_ENTERPRISE/Event/Name 
    | 
    /CALENDAR_ENTERPRISE/Event/Description 
    | 
    /CALENDAR_ENTERPRISE/Event/Categorization 
    | 
    /CALENDAR_ENTERPRISE/Event/Locations/Location/LocationName 
    | 
    /CALENDAR_ENTERPRISE/Event/StartDate 
    | 
    /CALENDAR_ENTERPRISE/Event/StartTime 
    | 
    /CALENDAR_ENTERPRISE/Event/EndDate 
    | 
    /CALENDAR_ENTERPRISE/Event/EndTime 
    | 
    /CALENDAR_ENTERPRISE/Event/ImportSeriesId 

這會精確選擇您在評論中表示爲所需XML文檔中的元素。

+0

我從XPathNodeIterators得不到任何結果,並且所有記錄都沒有任何數據通過位置組。它看起來像我的軸正在下降,事件低於事件第一個孩子 – 2010-08-13 14:40:01

+0

現在你明顯提供了一個新的XML文檔,我相應地更正了XPath表達式。嘗試使用新的XPath表達式。 – 2010-08-13 15:59:00

+0

我曾假設,但我得到的結果做了2件奇怪的事情。 1)沒有地點的任何事件根本沒有出現。 2)位置名稱字段下方的任何數據似乎無法訪問。 – 2010-08-13 17:14:03