如何最快地选择单个XML节点值

时间:2014-03-07 23:47:51

标签: xml linq datareader

我有以下xml

<?xml version='1.0'?>
<test>
    <exam paper="math" section="third" value="dull">
        <place>college</place> 
    </exam> 
</test>

我希望获得“section”的值并将“tag”置于最佳性能。我应该使用linq还是xmldatareader?请分享代码

1 个答案:

答案 0 :(得分:0)

XmlDataReader可能比LINQ to XML更快,但LINQ to XML可能更容易编写和维护。

我能够在0.1秒内运行以下代码一万次(10,000次):你需要比这更好的性能吗?

var doc = XDocument.Parse(
@"<?xml version=""1.0""?>
<test>
    <exam paper=""math"" section=""third"" value=""dull"">
        <place>college</place>
    </exam>
</test>");
var info = 
    (from test in doc.Elements("test")
    from exam in test.Elements("exam")
    select new{
        section = exam.Attribute("section").Value,
        place = exam.Element("place").Value
    })
    .ToList();
相关问题