I'm having trouble in the following code, in that, the XDocument contains NO elements and hence the LINQ query will not execute. I've posted the code and the XML below that. For the life of me I cannot work out why, when the XDocument is populated (this is the xml I have provided from a copy & paste out of visual studio). Any help as to why the Descendants will not find any of the elements I would be very grateful... Regards, Tim.
The Code
// 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;
The XML Document:-
<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>
The code you've posted is incomplete; what is query
? Your LINQ query should look like this:
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"),
};
or
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"),
};
The above code assumes you have a class called Picture
containing the properties Name
, Date
& Url
.
精彩评论