2011-09-06 193 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创建的。 –