Get all tags of the same tag name regardless of level in XML File with C#

Viewed 576

I have the following method and xml file below. The method filters the content of all "Tag4" elements and inserts it into a list. Unfortunately, I have the problem that with several child elements the method does not quite work anymore. Then only the content: "Content1" is added to the list and the rest is ignored.

Methode:

public List<string> Method(string sourcePath)
{
    string directory = sourcePath;
    var resultList = new List<string>();

    try
    {
        XDocument xdocument = XDocument.Load(directory);
        IEnumerable<XElement> tags = xdocument.Root.Elements().Elements().Where(x => x.Name.LocalName.Equals("Tag4"));
        foreach (var tag in tags)
        {
            var value = tag?.Value;
            if (!string.IsNullOrEmpty(value) && !resultList.Contains(value))
            {
                resultList.Add(value);
            }
        }
    }
    catch (Exception ex)
    {
    }
    return resultList;
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<Tag1Group xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsinc="http://www.w3.org/2001/XInclude" validFrom="2020-03-26T00:00:00+01:00" version="4" subversion="22" xmlns="http://www.bmw.com/ista/v2">
  <Tag1 name="Content1" version="4" Subversion="22">
    <Tag2>
      <Tag1 name="Content2" version="4" subversion="22">
        <Tag2>
          <Tag1 name="Content3" version="4" subversion="22">
            <Tag2>
              <Tag1 name="Content4" version="4" subversion="22">
                <Tag3>Ex</Tag3>
                <Tag4>Content4</Tag4>
                <Tag5>123</Tag5>
              </Tag1>
            </Tag2>
            <Tag3>Ex</Tag3>
            <Tag4>Content3</Tag4>
            <Tag5>456</Tag5>
          </Tag1>
        </Tag2>
        <Tag3>Ex</Tag3>
        <Tag4>Content2</Tag4>
        <Tag5>789</Tag5>
      </Tag1>
    </Tag2>
    <Tag3>Ex</Tag3>
    <Tag4>Content1</Tag4>
    <Tag5>1011</Tag5>
  </Tag1>
</Tag1Group>

How can I change the method to take all elements from the tag4 regardless of their level. So I also mean: "Content2", "Content3", "Content4"

2 Answers

Try using Descendants() method:

Returns a collection of the descendant elements for this document or element, in document order.

IEnumerable<XElement> tags = xdocument.Root.Descendants()
    .Where(e => e.Name.LocalName == "Tag4")

You do not even need linq for it. There is method Descendants() which allow you to get all elements with name that is passed as argument.

Example in your code:

        public List<string> Method(string sourcePath)
        {
            string directory = sourcePath;
            var resultList = new List<string>();

            try
            {
                XDocument xdocument = XDocument.Load(directory);
                var docNamespace = xdocument.Root.Name.Namespace;
                var tags = xdocument.Descendants(docNamespace + "Tag4");

                foreach (var tag in tags)
                {
                    var value = tag?.Value;
                    if (!string.IsNullOrEmpty(value) && !resultList.Contains(value))
                    {
                        resultList.Add(value);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return resultList;
        }
Related