2011-04-21 144 views
1

我是新來Visual Studio和我的頭很多東西,但我卡住,可以使用一些幫助。XML xpath視覺工作室

我需要創建綁定到XML文檔中的字段的下拉列表。我已經嘗試配置數據源並添加xpath表達式,但沒有顯示出來,我不知道我在做什麼錯誤。

這是從文件中提取的,並說我想要任何字段,即PropertyId

<PropertyDatabase> 

<imageList> 
    <Images> 
    <ImageId>2</ImageId> 
    <PropertyId>60</PropertyId> 
    <ThumbUrl>propertyImages/propertyThumb60_8.jpg</ThumbUrl> 
    <MainUrl>propertyImages/propertyLarge60_8.jpg</MainUrl> 
    <Active /> 
    </Images> 
    <Images> 

    <ImageId>3</ImageId> 
    <PropertyId>22</PropertyId> 
    <ThumbUrl>propertyImages/propertyThumb22_1.jpg</ThumbUrl> 
    <MainUrl>propertyImages/propertyLarge22_1.jpg</MainUrl> 
    <Active /> 
    </Images> 
+0

燦你請顯示你使用的代碼? – 2011-04-21 19:23:27

回答

4

您可以使用XElement的Linq-to-XML構造在C#中創建XML路徑查詢。 如果你的文件被稱爲「somexml.xml」那麼你可以做以下

XElement xml = XElement.Load("somexml.xml"); 
IEnumerable<XElement> propertyIDs = xml.Descendants("PropertyId"); 
foreach(XElement propertyID in propertyIDs) 
{ 
    //Do stuff with propertyID.Value 
} 

如您還沒有指定您正在使用C#,這裏是在VB.Net

代碼
Dim xml As XElement = XElement.Load("somexml.xml") 
Dim propertyIDs As IEnumerable(Of XElement) = xml...<PropertyId> 
For Each propertyID As XElement In propertyIDs 
    'Do stuff with propertyID.Value 
Next 
+1

+1用於開通XPath並轉向Linq。 – mattmc3 2011-04-21 19:27:05

+0

thx的答覆。在代碼使用方面 - 我沒有 - 我在Visual Studio中添加數據源時使用嚮導,然後嘗試通過那裏添加xpaths。 – Kev 2011-04-21 19:44:04

+0

然後你應該使用// PropertyId – 2011-04-21 19:48:27