C# 如何使用C从包含TextValue格式元素的xml中的多个节点读取值#
这是我们正在使用的示例xml,需要读取TextValue属性中的值C# 如何使用C从包含TextValue格式元素的xml中的多个节点读取值#,c#,C#,这是我们正在使用的示例xml,需要读取TextValue属性中的值 <SelectApplicableVehicle> <TextValue> <text>2000 - 57y-ry5465-Truck</text> <value>2000 - 57y-ry5465-Truck-21063</value>
<SelectApplicableVehicle>
<TextValue>
<text>2000 - 57y-ry5465-Truck</text>
<value>2000 - 57y-ry5465-Truck-21063</value>
</TextValue>
<TextValue>
<text>2008 - 57y-3546-Truck</text>
<value>2008 - 57y-3546-Truck-21064</value>
</TextValue>
</SelectApplicableVehicle>
2000-57y-ry5465-卡车
2000-57y-ry5465-Truck-21063
2008-57y-3546-卡车
2008-57y-3546-Truck-21064
试试xml linq:
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
var results = doc.Descendants("TextValue").Select(x => new
{
key = (string)x.Element("text"),
value = (string)x.Element("value")
}).ToList();
}
}
}
多了解一点就好了。你已经试过什么了。什么不起作用。在上面的XML中,TextValue是一个元素,而不是一个属性!