2011-04-21 142 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