0

How can I declare a variable inside a for loop

foreach (System.Xml.XmlNode xmlnode in node)
{
    string AMSListName = xmlnode.Attributes["Title"].Value.ToString();
    /* the below assignment should be variable for each iteration */
    XmlNode ndViewFields + AMSListName = xmlDoc.CreateNode(XmlNodeType.Element,
                                                           "ViewFields", "");
}

How do I achieve this? I want for each value in the for loop to have the xmlnode to have a different name. Is this possible at all?

5
  • 1
    Why? Perhaps a list with all of the nodes would be a better choice? Commented Mar 31, 2014 at 13:36
  • No, you cannot do this, unless perhaps with reflection. Use a collection to store your data in instead. Commented Mar 31, 2014 at 13:36
  • I don't quite understand. Do you want every variable to be named differently? Commented Mar 31, 2014 at 13:38
  • Can you describe the problem that you are trying to solve? It looks like you are reading 1 xml file and outputting a new one with a different schema. Is that right? Commented Mar 31, 2014 at 13:39
  • Even if this would work, how would you access these variables? Commented Mar 31, 2014 at 13:42

2 Answers 2

5

Use a collection:

List<XmlNode> nodes = new List<XmlNode>();
foreach (System.Xml.XmlNode xmlnode in node)
{
    string AMSListName = xmlnode.Attributes["Title"].Value.ToString();
    nodes.Add(xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", ""));
}

You can access this list via index or in a loop:

foreach(var node in nodes)
{
    // ...
}

another approach If the name is an identifier, use a Dictionary:

Dictionary<string, System.Xml.XmlNode> nodeNames = new Dictionary<string, System.Xml.XmlNode>();
foreach (System.Xml.XmlNode xmlnode in node)
{
    string AMSListName = xmlnode.Attributes["Title"].Value.ToString();
    nodeNames[AMSListName] = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
}

This will replace an already available node with a given name, otherwise it'll add it.

You can access it via name:

XmlNode node
if(nodeNames.TryGetValue("Some Name", out node)
{
    // ..
};
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest using something like a dictionary or hastable. Because even if you did create dynamic variables, how would you reference them later? I am not sure this is possible.

            Hashtable ViewFields = new Hashtable();

        foreach (System.Xml.XmlNode xmlnode in node)
        {
            string AMSListName = xmlnode.Attributes["Title"].Value.ToString();
            XmlNode nd = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
            ViewFields.Add(AMSListName,nd);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.