从隔离存储中获取xml XDocument,用于LINQ

本文关键字:XDocument 用于 LINQ xml 获取 隔离 存储 | 更新日期: 2023-09-27 18:07:36

我在下面的代码中遇到了麻烦,因为XDocument包含没有元素,因此LINQ查询将不会执行。我在下面发布了代码和XML。对于我的生活,我不能弄清楚为什么,当XDocument填充(这是我从副本提供的xml &粘贴出visual studio)。任何帮助,为什么后裔不会找到任何元素,我将非常感激…问候,蒂姆。

代码

// 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>

从隔离存储中获取xml XDocument,用于LINQ

您发布的代码不完整;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的类,它包含Name, Date &Url .