2011-09-06 216 views
0

我在下面的代碼中遇到了問題,因爲XDocument包含NO元素,因此LINQ查詢將不會執行。我已經發布了代碼和下面的XML。對於我的生活,我不能解決爲什麼,當XDocument填充(這是我從一個複製&粘貼提供的視覺工作室提供的XML)。任何幫助,爲什麼後代將找不到任何我會非常感激的元素...問候,蒂姆。xml來自isolationstorage的XDocument,用於LINQ

守則

// load all of the pictures into the list so we can see the details 
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
XDocument loadedData = XDocument.Load(isoStore.OpenFile(App._PicturesConfig,System.IO.FileMode.Open)); 
var data = from query in loadedData.Descendants("Picture") 
select new Picture    
{     
    Name = (string)query.Element("Name"), 
    Date = (string)query.Element("Date"),     
    Url = (string)query.Element("Url")    
}; 
lb_picListBox.ItemsSource = data; 

XML文檔: -

<ArrayOfPicture xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Picture> 
    <Name>FishCleaning</Name> 
    <Date>20110905</Date> 
    <Desc>Cleaning behaviour of blue-striped grunt and four-eyed butterfly fish, Florida Keys National Marine Sanctuary, USA -- Stephen Frink/Corbis</Desc> 
    <Url>ArtGalleryPhotos\FishCleaning20110905</Url> 
    </Picture> 
    <Picture> 
    <Name>Stephansdom</Name> 
    <Date>20110904</Date> 
    <Desc>Roof of the Stephansdom (St Stephen’s) Cathedral, Vienna, Austria -- Walter Bibikow/Corbis</Desc> 
    <Url>ArtGalleryPhotos\Stephansdom20110904</Url> 
    </Picture> 
    <Picture> 
    <Name>RegataStorica</Name> 
    <Date>20110903</Date> 
    <Desc>Historical regatta in Venice, Italy -- SIME/eStock Photo</Desc> 
    <Url>ArtGalleryPhotos\RegataStorica20110903</Url> 
    </Picture> 
    <Picture> 
    <Name>PearlMonument</Name> 
    <Date>20110902</Date> 
    <Desc>The pearl monument in the Corniche neighbourhood of Doha, Qatar, illuminated at night with the new high-rises of West Bay in the background -- Jon Hicks/Corbis</Desc> 
    <Url>ArtGalleryPhotos\PearlMonument20110902</Url> 
    </Picture> 
    <Picture> 
    <Name>LondonSkyline</Name> 
    <Date>20110901</Date> 
    <Desc>Tower Bridge at night, London -- Jason Hawkes/Getty Images</Desc> 
    <Url>ArtGalleryPhotos\LondonSkyline20110901</Url> 
    </Picture> 
    <Picture> 
    <Name>YunnanProvince</Name> 
    <Date>20110831</Date> 
    <Desc>Aerial view of rice fields in Yunnan Province, China -- Jialiang Gao/Getty Images</Desc> 
    <Url>ArtGalleryPhotos\YunnanProvince20110831</Url> 
    </Picture> 
    <Picture> 
    <Name>KarnasaiValley</Name> 
    <Date>20110830</Date> 
    <Desc>Pinnacles of sandstone among orange dunes of the Karnasai Valley, Chad -- George Steinmetz/Corbis</Desc> 
    <Url>ArtGalleryPhotos\KarnasaiValley20110830</Url> 
    </Picture> 
    <Picture> 
    <Name>SaddlebackCaterpillar</Name> 
    <Date>20110829</Date> 
    <Desc>Saddleback caterpillar on a leaf in the Cockscomb Basin, Belize -- Frans Lanting/Corbis</Desc> 
    <Url>ArtGalleryPhotos\SaddlebackCaterpillar20110829</Url> 
    </Picture> 
    <Picture> 
    <Name>NottingHill</Name> 
    <Date>20110828</Date> 
    <Desc>Notting Hill Carnival, London, England -- Bettina Strenske/Photolibrary</Desc> 
    <Url>ArtGalleryPhotos\NottingHill20110828</Url> 
    </Picture> 
    <Picture> 
    <Name>KeeBeach</Name> 
    <Date>20110827</Date> 
    <Desc>Wave breaking off Ke'e Beach on Kauai, Hawaii, USA -- Mark A. Johnson/Corbis</Desc> 
    <Url>ArtGalleryPhotos\KeeBeach20110827</Url> 
    </Picture> 
    <Picture> 
    <Name>ChileVolcano</Name> 
    <Date>20110826</Date> 
    <Desc>The eruption of Puyehue - Cordon Caulle Rininahue Volcano, Chile -- Ian Salas/Photolibrary</Desc> 
    <Url>ArtGalleryPhotos\ChileVolcano20110826</Url> 
    </Picture> 
    <Picture> 
    <Name>StaghornCoral</Name> 
    <Date>20110825</Date> 
    <Desc>Staghorn coral, Great Barrier Reef, off the coast of Australia -- Frans Lanting/Corbis</Desc> 
    <Url>ArtGalleryPhotos\StaghornCoral20110825</Url> 
    </Picture> 
    <Picture> 
    <Name>LacewingEggs</Name> 
    <Date>20110824</Date> 
    <Desc>Green lacewing eggs -- Charles Melton/Visuals Unlimited</Desc> 
    <Url>ArtGalleryPhotos\LacewingEggs20110824</Url> 
    </Picture> 
    <Picture> 
    <Name>Happisburgh</Name> 
    <Date>20110823</Date> 
    <Desc>Sea walls built off the coast of Happisburgh, England -- Tim Harris</Desc> 
    <Url>ArtGalleryPhotos\Happisburgh20110823</Url> 
    </Picture> 
    <Picture> 
    <Name>OatHarvest</Name> 
    <Date>20110822</Date> 
    <Desc>Combine harvesting oats surrounded by a corn field in Wisconsin -- Richard Hamilton Smith/Corbis</Desc> 
    <Url>ArtGalleryPhotos\OatHarvest20110822</Url> 
    </Picture> 
    <Picture> 
    <Name>CasaresSpain</Name> 
    <Date>20110821</Date> 
    <Desc>The white village of Casares, Spain -- Jose Fuste Raga/Corbis</Desc> 
    <Url>ArtGalleryPhotos\CasaresSpain20110821</Url> 
    </Picture> 
    <Picture> 
    <Name>NebutsujiTemple</Name> 
    <Date>20110820</Date> 
    <Desc>Stone statues in Otagi Nenbutsu-ji Temple in Kyoto, Japan -- Rudy Sulgan/Corbis</Desc> 
    <Url>ArtGalleryPhotos\NebutsujiTemple20110820</Url> 
    </Picture> 
    <Picture> 
    <Name>AfricanElephant</Name> 
    <Date>20110819</Date> 
    <Desc>Juvenile African elephant with cattle egrets -- Anup Shah/Corbis</Desc> 
    <Url>ArtGalleryPhotos\AfricanElephant20110819</Url> 
    </Picture> 
    <Picture> 
    <Name>AircraftBoneyard</Name> 
    <Date>20110818</Date> 
    <Desc>Jet aircraft on the tarmac of the Davis-Monthan Air Force Base in Tucson, Arizona, USA -- Jay Dickman/Corbis</Desc> 
    <Url>ArtGalleryPhotos\AircraftBoneyard20110818</Url> 
    </Picture> 
    <Picture> 
    <Name>GuilinFisherman</Name> 
    <Date>20110817</Date> 
    <Desc>Fisherman near Guilin, China -- SIME/eStock Photo</Desc> 
    <Url>ArtGalleryPhotos\GuilinFisherman20110817</Url> 
    </Picture> 
</ArrayOfPicture> 
+1

發佈您的所有代碼。 'query'變量缺失,'select new Picture'語句根本沒有意義! –

+0

克勞斯感謝您的評論(道歉我昨天錯過了!)。我修改了我的複製和粘貼錯誤。還添加了下面的評論添加到評論 –

回答

1

你已經發布的代碼是不完整的;什麼是query?你的LINQ查詢應該是這樣的:

var pictures = from p in loadedData.Descendants("Picture") 
       select new Picture() { 
       Name = (string)p.Element("Name"), 
       Date = (string)p.Element("Date"),     
       Url = (string)p.Element("Url"), 
       }; 

var pictures = from p in loadedData.Root.Elements("Picture") 
       select new Picture() { 
       Name = (string)p.Element("Name"), 
       Date = (string)p.Element("Date"),     
       Url = (string)p.Element("Url"), 
       }; 

上面的代碼假設你有一個名爲Picture包含屬性NameDate & Url類。

+0

Praetorian - 道歉我做了一個複製和粘貼錯誤。我已經糾正了這個問題,因爲你可以在上面的原始文件中看到,因此現在它讀取simialr到你的答案。然而,這裏的關鍵是在調試模式下,loadedData.Root.Elements(「Picture」)或loadedData.Descendants(「Picture」)都不會返回任何東西(例如,如果你看着它們,它們會返回'null'....謝謝你的幫助...... –

+0

@Tim Windsor:我的猜測是文檔本身沒有被加載,在'XDocument.Load'行後面添加一個斷點並在調試器窗口中觀察它,如果它正確加載,你應該能夠看到XML文件的全部內容我在WPF項目中嘗試了上面的代碼(沒有在這臺機器上安裝Windows Phone編譯器),這兩個版本都工作得很好。第一個地方? – Praetorian

+0

我上面粘貼的文檔是XDocument變量的內容(loadedData)。這就是爲什麼我很困惑。這個XML文檔是使用Picture類列表(我已經創建)的XmlSerializer創建的。 –