XmlTextReader vs. XDocument

前端 未结 2 1949
花落未央
花落未央 2020-11-30 06:45

I\'m in the position to parse XML in .NET. Now I have the choice between at least XmlTextReader and XDocument. Are there any comparisons between th

2条回答
  •  既然无缘
    2020-11-30 07:35

    If you're happy reading everything into memory, use XDocument. It'll make your life much easier. LINQ to XML is a lovely API.

    Use an XmlReader (such as XmlTextReader) if you need to handle huge XML files in a streaming fashion, basically. It's a much more painful API, but it allows streaming (i.e. only dealing with data as you need it, so you can go through a huge document and only have a small amount in memory at a time).

    There's a hybrid approach, however - if you have a huge document made up of small elements, you can create an XElement from an XmlReader positioned at the start of the element, deal with the element using LINQ to XML, then move the XmlReader onto the next element and start again.

提交回复
热议问题