Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
and my xml code is
XML
<scxml> 
       <state id="appear"> 
               <onentry>
	<send delay="2000ms" event="viman_1" target="target">
	<send event="viman_1" delay="2000ms">\
		
              <transition event="born" ontransit="say_hello" target="target"> 
              <transition event="viman_1" ontransit="say_hello" target="live"> 
		 <parallel id="live"> 
                    <transition event="hp_zero" target="dead"> 
                        <state id="eat"> 
                         
                         <state id="move"> 
                          
                 
			  
		<onexit>
		 <log expr="'onexit: TS0_s01'">
				
	 
   
		
       <final id="dead">'


What I have tried:

C#
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); //my function
        }

    }
}
Posted
Updated 19-Jun-19 0:21am
v2

1 solution

Use this:
XContainer.Elements Method (System.Xml.Linq) | Microsoft Docs[^] instead of Descendants if you are only looking for one level.

Something else:
IEnumerable<xelement> de = from el in xdoc.Descendants() select el;

doesn't add anything besides making the code look more complex. It basically says "Make a collection b that is containing all items in collection a". Why not just use collection a?

So you can delete the line, and instead loop on:
foreach (XElement el in  xdoc.Descendants())

or more likely what you want in this specific case:
foreach (XElement el in  xdoc.Root.Elements())
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900