Reading XML and performing an action based on attribute

Let's say I have an XML file, for example:

<root>
  <level1 name="level1A">
    <level2 name="level2A">
      <level3 name="level3A">
        <level4 name="level4A">
          <level5 name="level5A">
            <level6 name="level6A">
              <level7 name="level7A">
                <level8 name="level8A"></level8>
              </level7>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
   <level1 name="level1B">
     <level2 name="level2B">
       <level3 name="level3B">
        <level4 name="level4B">
          <level5 name="level5B">
            <level6 name="level6B">
              <level7 name="level7B">
                <level8 name="level8B"></level8>
              </level7>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
</root>

How can I read this file and execute a piece of code depending on the element? for example, if the "name" element says "level7a", execute the X code snippet. If the name element says level7B, execute the Y code snippet.

I can provide such code snippets if t makes it easier to answer the question. Thanks for the help!

+5
source share
3 answers

You can create Dictionary<string, Action>one that maps attribute names to actions. Then, by analyzing the xml, you can find the fragment in the dictionary and execute it.

Quick example:

var attributeActions = new Dictionary<string, Action>();
attributeActions["level1A"] = () => { /* do something */ };
attributeActions["level2A"] = () => { /* do something else */ };

...
// call it
attributActions[node.Attributes["name"]]();

, , , :

public static void Execute<TKey>(this IDictionary<TKey, Action> actionMap, TKey key)
{
    Action action;
    if (actionMap.TryGet(key, out action))
    {
         action();
    }
}

:

attributActions.Execute(node.Attributes["name"]);

Action ( void), Action<T> Func<T, R>, / .

+5

, 2 :

  • () , ,

  • .

:

  var doc =  System.Xml.Linq.XDocument.Load(fileName);       
  Visit(doc.Root);


    private static void Visit(XElement element)
    {
        string name = element.Attribute("name").Value;
        Execute(name);

        // you seem to have just 1 child, this will handle multiple
        // adjust to select only elements with a specific name 
        foreach (var child in element.Elements())
            Visit(child);
    }


    private static void Execute(string name)
    {
        switch (name)
        {
            case "level1A" :
                // snippet a
                break;

            // more cases
        }
    }
+4

@ChrisWue. , , :

private Dictionary<String, Action> actionList = new Dictionary<string, Action>();

private void method1() { }
private void method2() { }

private void buildActionList() {
    actionList.Add("level7a", new Action(method1));
    actionList.Add("level7B", new Action(method2));
    // .. etc
}

public void processDoc() {
    buildActionList();
    foreach (XElement e in (XDocument.Parse(File.ReadAllText(@"C:\somefile.xml")).Elements())) {
        string name = e.Attribute("name").Value;
        if (name != null && actionList.ContainsKey(name)) {
            actionList[name].Invoke();
        }
    }
}

.. , - 1, method2 ..

+3

All Articles