2011-09-23 73 views
0

我無法填充XML文件中的對象。我已經複製了一個我幾乎完全找到的示例,並更改了變量名稱,但是我一直收到「枚舉未找到結果」異常。LINQ to XML。枚舉沒有結果

這是我的代碼:

Dim element As XElement = XElement.Load(path) 

    Dim itemProps = From p In element...<Property> _ 
       Where p.<LanguageCode>.Value = "en_us" _ 
       Select p.<Title>.Value, p.<Description>.Value 

使用斷點,我已經證實,「元素」變量是使用XElement.Load(path)方法被正確填充。

這裏是正在訪問的XML文件:

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<Items> 
    <Item ItemID="1"> 
    <Property ItemPropertyID="1"> 
     <Title>Title1</Title> 
     <Description>Description1</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
    <Item ItemID="2"> 
    <Property ItemPropertyID="2"> 
     <Title>Title2</Title> 
     <Description>Description2</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
    <Item ItemID="3"> 
    <Property ItemPropertyID="3"> 
     <Title>Title3</Title> 
     <Description>Description3</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
    <Item ItemID="4"> 
    <Property ItemPropertyID="4"> 
     <Title>Title4</Title> 
     <Description>Description4</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
    <Item ItemID="5"> 
    <Property ItemPropertyID="5"> 
     <Title>Title5</Title> 
     <Description>Description5</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
    <Item ItemID="6"> 
    <Property ItemPropertyID="6"> 
     <Title>Title6</Title> 
     <Description>Description6</Description> 
     <LanguageCode>en-us</LanguageCode> 
    </Property> 
    </Item> 
</Items> 

本質上來說,XML查詢應該返回標題,並且具有所謂的語言代碼元素的每個屬性的說明,這等於到「en-us」。我有一種感覺,我的問題在於我的XML代碼,但我現在已經陷入了很長時間。請幫忙。

回答

2

「en_us」應該是「en-us」:)。

糟糕!您的答案無法提交,因爲:

身體必須至少有30個字符;你輸入了28

那裏。現在它超過了28. :)

+0

+1,但你不配作弊SO:P – alf

+0

哇,我不敢相信我錯過了那個。 – user961794

+0

Holy C ** p我也不能......我沒有看到那個人...... *拍拍自己* :-) – shawty

0

嘗試採取點的一個出

Dim itemProps = From p In element...<Property> 

你去3級了,當你只需要往下走2

如果不行嘗試只是一個點,因爲基本上你的旅行路徑在文檔根目錄下只有1個。

+0

1個點不起作用,2個點給我一個「標識符預期」構建錯誤。 – user961794

+0

我真的不認爲點數代表XML文件中的深層次,但僅僅是LINQ to SQL語法的一部分。 – user961794

+0

如果知道真相,我會隨心所欲地出去走走,但我通常不會做VB,所以有時候在C#中語法會拋出我,但是嵌套層次很重要,但它不是用多個。而是用元素/元素調用的多重嵌套。 – shawty