Pregunta

I have the following xml

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

and i want to get the value of "section" and place "tag" with best performance. Shall i use linq or xmldatareader? please share the code also

¿Fue útil?

Solución

XmlDataReader would probably be faster than LINQ to XML, but LINQ to XML will probably be easier to write and maintain.

I was able to run the following code ten thousand (10,000) times in just over 0.1 seconds: do you need better performance than that?

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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top