c# - Reading an xml file without knowing the name of the tags -


i need read xml file without knowing name of tags. using xelement.i need find efficient way make program. xml:

<actions> <action name="action1" id="ae6ecd81-6cc1-4a05-b379-113d700bd1a9">     <trigger name="alo">         <and>             <checked id="3897d8c6-873f-45ad-8d28-1a80b8ac5cbd" parameter="attribute" value="checked"/>             <checked id="b1ba57cd-bfaf-4c52-8686-f7073f68ef71" parameter="attribute" value="unchecked"/>         </and>     </trigger>     <command>         <true>             <do id="f9129376-dcde-4964-8b5c-df0ef8886ab9" parameter="visible" value="true"/>             <do id="456627b0-8195-459c-981f-7450866ce635" parameter="visible" value="true"/>         </true>     </command> </action> <action name="action2" id="5e7809ff-feab-48f2-9f18-0e67f514afb2">     <trigger>         <or>             <checked id="3897d8c6-873f-45ad-8d28-1a80b8ac5cbd" parameter="attribute" value="checked"/>             <checked id="b1ba57cd-bfaf-4c52-8686-f7073f68ef71" parameter="attribute" value="unchecked"/>         </or>     </trigger>     <command>         <true>             <do id="f9129376-dcde-4964-8b5c-df0ef8886ab9" parameter="visible" value="true"/>             <do id="456627b0-8195-459c-981f-7450866ce635" parameter="visible" value="true"/>         </true>     </command> </action> 

my c# code:

    public override void loadfromxml(string xmlfileaddress)     {         xdocument doc = xdocument.load(xmlfileaddress);         var actions = doc.descendants("action").tolist();         foreach (var in actions)         {             xmlstring = a.tostring();             atcion hta = new atcion();             hta.loadfromxml(xmlstring);             actionlist.add(hta);          }      }      list<trigger> triggerlist = new list<trigger>();     list<command> commandlist = new list<command>();     string actname;     string actid;     string trigname;     string actype;     public override void loadfromxml(string xmlfileaddress)     {         string xmlstr = "";         xelement elem = xelement.parse(xmlfileaddress);                     actid = elem.attribute("id").value;         actname = elem.attribute("name").value;         var triggers = elem.descendants("trigger").tolist();                                 foreach (var t in triggers)         {             trigname = t.attribute("name").value;             actype = t.element("and").name.localname;             var chaction = t.descendants("and").tolist();                             foreach (var c in chaction)             {                 trigger tri = new trigger();                 xmlstr = c.tostring();                 tri.loadfromxml(xmlstr);                 triggerlist.add(tri);             }         } 

the problem have fact need know name of tags in xml. grateful if tells me how can read tags without knowing name , offers me efficient way so.

i assuming have prior knowledge of action, trigger , command , want parse actions dynamically.

var xdoc = xdocument.load(filename);  var actions = xdoc.descendants("action")                     .select(action => new                     {                         name = action.attribute("name").value,                         trigger = new                         {                             name = (string)action.element("trigger").attribute("name"),                             conditions = action.element("trigger")                                             .elements().first()                                             .elements()                                             .select(e => new{                                                 name = e.parent.name.localname,                                                 attributes = e.attributes().todictionary(a=>a.name.localname,a=>a.value)                                             })                                             .tolist()                          },                         command = new                         {                             name = (string)action.element("trigger").attribute("name"),                             = action.element("command")                                        .elements().first()                                        .elements()                                        .select(e => new                                         {                                             name = e.parent.name.localname,                                             attributes = e.attributes().todictionary(a => a.name.localname, => a.value)                                          })                                         .tolist()                          },                     })                     .tolist(); 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -