0

I have a webservice that returns this string value:

<NewDataSet>
  <Table>
    <Country>Mexico</Country>
    <City>Acapulco / G. Alvarez</City>
  </Table>
  <Table>
    <Country>Mexico</Country>
    <City>Aerop. Internacional Monterrey, N. L.</City>
  </Table>
  <Table>
    <Country>Mexico</Country>
    <City>Aguascalientes, Ags.</City>
  </Table>
</NewDataSet>

I need to insert the City values in a database table. What I intend to do is search the entire string and everytime it detects a tag , I want to extract the value inside and then I would store each value in a List for later use.

However I have not been able to find a way to repeat the process various times on the same string.

I first tried Substring and Split with no success yet. Do you know a method that help me solve this problem? Thanks in advance.

4

2 回答 2

2
var cities = XDocument.Parse(myString).Descendants("City").Select(d => d.Value);

这是使用 LINQ to XML 获取值的示例。一般来说,您的问题基本上是您有一个 XML 字符串,您需要以某种方式解析并获取适当的值。其他一些常用方法可能是使用 XmlDocument 或将您的 XML 反序列化为类结构。

于 2013-01-27T23:50:51.497 回答
0

使用 Linq to XML 或类似的 XML 解析:

http://msdn.microsoft.com/en-us/library/bb299195.aspx

XElement stringAsXml = XElement.Parse(myString);
于 2013-01-27T23:51:44.773 回答