문제

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

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top