简体   繁体   English

XML xpath Visual Studio

[英]XML xpath Visual Studio

I am new to Visual Studio and getting my head around a lot of things but I'm stuck and could use some help. 我是Visual Studio的新手,能够解决很多问题,但我已经陷入困境,可以使用一些帮助。

I need to create a drop down list bound to a field in an XML document. 我需要创建一个绑定到XML文档中的字段的下拉列表。 I've tried configuring the data source and adding xpath expressions, but nothing shows up and I am not sure what I am doing wrong. 我已经尝试配置数据源并添加xpath表达式,但没有显示任何内容,我不确定我做错了什么。

Here is extract from file, and say I wanted any field, ie PropertyId ? 这是从文件中提取,并说我想要任何字段,即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>

You can use the Linq-to-XML construct of XElement to create XML path like queries in C#. 您可以使用XElement的Linq-to-XML构造在C#中创建类似查询的XML路径。 If your file is called 'somexml.xml' then you can do the following 如果您的文件名为“somexml.xml”,则可以执行以下操作

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

As you have not specified that you are using C#, here is the code in VB.Net 由于您没有指定使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM