Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自isolatedstorage的xml XDocument,用于LINQ_C#_Linq_Windows Phone 7_Isolatedstorage - Fatal编程技术网

C# 来自isolatedstorage的xml XDocument,用于LINQ

C# 来自isolatedstorage的xml XDocument,用于LINQ,c#,linq,windows-phone-7,isolatedstorage,C#,Linq,Windows Phone 7,Isolatedstorage,我在下面的代码中遇到了问题,因为XDocument不包含任何元素,因此LINQ查询将不会执行。我已经在下面发布了代码和XML。在我的一生中,我无法理解为什么会填充XDocument(这是我从VisualStudio的复制粘贴中提供的xml)。任何关于后代为什么找不到任何元素的帮助我都会非常感激。。。你好,蒂姆 代码 // load all of the pictures into the list so we can see the details IsolatedStorageFile iso

我在下面的代码中遇到了问题,因为XDocument不包含任何元素,因此LINQ查询将不会执行。我已经在下面发布了代码和XML。在我的一生中,我无法理解为什么会填充XDocument(这是我从VisualStudio的复制粘贴中提供的xml)。任何关于后代为什么找不到任何元素的帮助我都会非常感激。。。你好,蒂姆

代码

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

鱼类清洗
20110905
美国佛罗里达州基斯国家海洋保护区蓝条纹咕噜鱼和四眼蝴蝶鱼的清洁行为——Stephen Frink/Corbis
ArtGalleryPhotos\FishCleaning20110905
圣斯特凡大教堂
20110904
奥地利维也纳斯蒂芬斯大教堂屋顶——沃尔特·比比科夫/科尔比斯
ArtGalleryPhotos\Stephansdom20110904
皇家酒店
20110903
意大利威尼斯历史赛船会——西蒙/埃斯托克照片
艺术画廊照片\RegataStorica20110903
珍珠纪念碑
20110902
位于卡塔尔多哈科尼切街区的珍珠纪念碑,在夜晚以西湾的新高楼为背景照亮——乔恩·希克斯/科尔比斯
艺术画廊照片\r\n 20110902
伦敦天际线
20110901
伦敦夜晚的塔桥——杰森·霍克斯/盖蒂图片社
ArtGalleryPhotos\LondonSkyline20110901
云南省
20110831
中国云南省稻田鸟瞰图——高嘉良/盖蒂图像
艺术画廊照片\云南省20110831
卡纳塞瓦利
20110830
乍得卡纳塞河谷橙色沙丘中的砂岩尖顶——乔治·斯坦梅茨/科尔比斯
艺术画廊照片\karnasaivalley 20110830
鞍背毛虫
20110829
伯利兹Cockscomb盆地叶子上的马鞍形毛虫——弗兰斯·兰廷/科尔比斯
ArtGalleryPhotos\SaddlebackCaterpillar20110829
诺丁希尔
20110828
英国伦敦诺丁山狂欢节——贝蒂娜·斯特兰斯基/摄影图书馆
艺术画廊照片\n NottingHill20110828
基比奇
20110827
美国夏威夷考艾岛科伊海滩的波浪破碎——马克·A·约翰逊/科尔比斯
ArtGalleryPhotos\KeeBeach20110827
奇列沃尔卡诺
20110826
Puyehue火山喷发——智利Cordon Caulle Rininahue火山——Ian Salas/照片库
艺术画廊照片\ChileVolcano20110826
鹿角珊瑚
20110825
澳大利亚海岸外的大堡礁鹿角珊瑚——弗兰斯·兰廷/科尔比斯
艺术画廊照片\StaghornCaral20110825
花边鸟
20110824
绿色草蛉蛋——查尔斯·梅尔顿/视觉无限
艺术画廊照片
哈比斯堡
20110823
英国哈皮斯堡海岸外修建的海堤——蒂姆·哈里斯
艺术画廊照片\Happisburgh20110823
燕麦收获
20110822
在威斯康星州的玉米田周围,联合收割燕麦——理查德·汉密尔顿·史密斯/科尔比斯
艺术画廊照片\r\n 20110822
西班牙卡萨雷斯酒店
20110821
西班牙卡萨雷斯的白人村庄——何塞·福斯特·拉加/科尔比斯
艺术画廊照片\casarespain20110821
内布苏吉坦普尔酒店
20110820
日本京都大谷能步寺的石像——鲁迪·苏尔根/科尔比斯
ArtGalleryPhotos\nButsujiTemple2010820
非洲象
20110819
幼年非洲象与牛白鹭——阿努普·沙阿/科尔比斯
ArtGalleryPhotos\AfricanElephant20110819
飞机墓地
20110818
美国亚利桑那州图森市戴维斯-蒙坦空军基地停机坪上的喷气式飞机——杰伊·迪克曼/科尔比斯
ArtGalleryPhotos\Aircraft Boneyard20110818
桂林谢尔曼
20110817
中国桂林附近的渔民——SIME/eStock照片
ArtGalleryPhotos\Guilifinisherman20110817

您发布的代码不完整;什么是
查询
?您的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"),
               };


上面的代码假设您有一个名为
Picture
的类,其中包含属性
Name
Date
&
Url

发布您的所有代码。
query
变量缺失,
selectnewpicture
语句毫无意义!克劳斯:谢谢你的评论(很抱歉我昨天错过了!)。我修改了复制粘贴错误。下面还添加了一些评论,这些评论添加到了Praetorian的评论中——我犯了复制粘贴错误,对此表示歉意。我已经纠正了这一点,正如你在上面的原文中所看到的,因此它现在读起来与你的答案类似。然而,这里的关键是,在调试模式下,loadedData.Root.Elements(“图片”)或loadedData.Substands(“图片”)都不会返回任何内容(即,如果你观察它们,它们会返回“null”……谢谢你的帮助……@Tim Windsor:我猜文档本身没有被加载。在
XDocument.Load
行后添加一个断点,并在调试器窗口中观察。如果加载正确,你应该能够看到XML文件的全部内容。我在WPF项目(这台机器上没有安装Windows Phone编译器),两个版本都可以正常工作。XML文件首先是如何将其放入IsolatedStorage的?我上面粘贴的文档是XDocument变量(loadedData)的内容。这就是我感到困惑的原因。XML文档是使用Picture类列表的XmlSerializer创建的(我已创建)…@Tim Windsor:尝试从根元素中删除架构名称空间声明。Silverlight不支持架构验证,因此它们在手机上毫无用处,可能这就是造成解析混乱的原因。
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"),
               };