在.NET 2.0中将String读取为XML

Chi*_*hin 0 xml vb.net .net-2.0

我有一个string来自DB的.我想读这个XML.字符串如下所示

<settings>
  <setting name="OfferIDs" value="47,48,49,50,51,52,53,76,77,78,79" />
  <setting name="someothersetting" value="" />
  <setting name="anothersetting" value="" />
</settings>
Run Code Online (Sandbox Code Playgroud)

我想OfferIDs使用VB.NET 获取字符串的值.提前谢谢了.

Jon*_*eet 5

编辑:要使用.NET 2,我可能会使用XmlDocument:

Dim document = new XmlDocument()
document.LoadXml(xml)
Run Code Online (Sandbox Code Playgroud)

然后,您需要浏览文档,通过其name属性查找相应的元素,然后获取该value元素的属性.XmlDocument这些天我生锈了,但希望这足以让你开始......


最简单的方法可能是使用LINQ to XML加载它:

Dim settings = XElement.Parse(xml)
Run Code Online (Sandbox Code Playgroud)

...然后查询它.在C#中很容易,但我的VB-fu让我失败了LINQ查询部分.

C#会是这样的:

XElement settings = XElement.Parse(xml);
string offerIds = settings.Elements("setting")
                          .Where(x => (string) x.Attribute("name") == "OfferIDS")
                          .Select(x => (string) x.Attribute("value"))
                          .Single();
Run Code Online (Sandbox Code Playgroud)