How we can just call the child element f scxml not (child of child)?

51 Views Asked by At

I am making a parser for state chart XML using C#. As we know there may be 2 children of state chart XML state and parallel so I make 2 functions one for state and other for parallel. How just I can call children of scxml not child of its child in my code it is calling all child+grandchild+grand_grand and so on.so Please some one explain how to just call child of scxml

I have tried to call its child but all its child comming

public static void Main(string[] args)
{
    var 
    xdocXDocument.Load(@"C:/Users/path.xml");

    IEnumerable<XElement> de = from el in xdoc.Descendants() select el;
    foreach (XElement el in de)
    {
        if (string.Equals(el.Name.ToString(), "state", StringComparison.InvariantCultureIgnoreCase))
        {
            stat(el);
        }
        else if (string.Equals(el.Name.ToString(), "parallel", StringComparison.InvariantCultureIgnoreCase))
        {
            parr(el);
        }

    }
}

Error is that in my xml one child of xml is state and state child is parallel so its also calling child of child. xml code is

1

There are 1 best solutions below

1
jbeard4 On

I think you want to call .Nodes, rather than .Descendants, to get the child nodes, rather than all descendant nodes (eg grandchildren, etc). You can find documentation on this here: https://learn.microsoft.com/en-us/dotnet/api/system.xml.linq.xcontainer.nodes?view=netframework-4.8#System_Xml_Linq_XContainer_Nodes