Convert string to variable name

前端 未结 5 485
逝去的感伤
逝去的感伤 2020-12-20 16:53

I have an XML file, I have a node and I read all ChildNodes. The name of the childNode match to a variable I have to set with the value of this childNode.

In the loo

5条回答
  •  心在旅途
    2020-12-20 17:27

    If you are seeking to assign variables based on the names of nodes in XML, you have at least a couple options:

    • Deserialize the XML structure into an object with corresponding member names
    • Populate the variables using reflection
    • Populate the variables using dynamic method calls/expression trees that know how to read the contents of the XML node into an object property.

    All of these approaches suggest a more object-oriented approach to the problem then just populating a few variables, but it would be easy to create a lightweight structure with the appropriate members which is populated by reading the XML document.

    You could also use a key-based collection (like a Dictionary) to store the values if you are just looking to build a simple name/value collection from the source XML.

提交回复
热议问题