How can I iterate though each child node in an XML file?

后端 未结 7 903
说谎
说谎 2021-01-04 00:49

I have an XML File and i would like to iterate though each child node gathering information.

Here is my C# code it only picks up one node, the FieldData i would like

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-04 01:34

    To iterate through each and every child node, sub-child node and so on, We have to use Recursion. In case someone have the same requirement as I had, I achieved this something like below -

    public string ReadAllNodes(XmlNode node)
    {
        if (node.ChildNodes.Count > 0)
        {
            foreach (XmlNode subNode in node)
            {
                //Recursion
                ReadAllNodes(subNode);
            }
        }
        else //Get the node value.
        {
            finalText = finalText + node.InnerText + System.Environment.NewLine;
        }
        return finalText;
    }
    

提交回复
热议问题