Walking an XML tree in C#

后端 未结 4 900
挽巷
挽巷 2021-02-19 22:35

I\'m new to .net and c#, so I want to make sure i\'m using the right tool for the job.

The XML i\'m receiving is a description of a directory tree on another machine, so

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 23:12

    This is a problem which is very suitable for recursion.

    To elaborate a bit more on what another poster said, you'll want to start by loading the XML into a System.Xml.XmlDocument, (using LoadXml or Load).

    You can access the root of the tree using the XmlDocument.DocumentElement property, and access the children of each node by using the ChildNodes property. Child nodes returns a collection, and when the Collection is of size 0, you know you'll have reached your base case.

    Using LINQ is also a good option, but I'm unable to elaborate on this solution, cause I'm not really a LINQ expert.

    As Jon mentioned, XmlReader isn't very friendly. If you end up having perf issues, you might want to look into it, but if you just want to get the job done, go with XmlDocument/ChildNodes using recursion.

提交回复
热议问题