Link to home
Start Free TrialLog in
Avatar of pothireddysunil
pothireddysunil

asked on

Create a class property using a variable.

I have a class and it has properties defied for creating an xml file.

I have another xml file which defines all the different elements information for the xml file generation for different kind of feeds.

See my code below.

Here i am reading a feed structure xml file.
I have a dictionary which has the element values for the xml file.
I am reading the structure xml file by tags and reading all the elements under those tags.
I have a loop here and comparing xml element with dictionary element and when both matches, i have to assign dictionary value to the class property.

I have class properties defined like xml.Library, xml.Application etc.

Suppose when both childNode.InnerText == dict.ElementAt(i).Key.ToString() matches

i have assign them like xml.Library = dict.ElementAt(i).Key.ToString().

But instead of specifying xml.Library i want to make it dynamic by specifying like

string str = dict.ElementAt(i).Key.ToString();
  "xml." + str = dict.ElementAt(i).Value.ToString();

Anyone has any suggessions, needed help it urgently. Thanks

 string strFile = "di-feedstructure.xml";
AIXML xml = new AIXML();
  XmlDocument xmldoc = new XmlDocument();
 xmldoc.Load(AppDomain.CurrentDomain.BaseDirectory + strFile);
 List<XmlNode> nodes = xmldoc.GetElementsByTagName("DiConfig").Cast<XmlNode>().ToList();
 foreach (XmlNode node in nodes)
 {
      if (node.Attributes["Feed"].InnerText == sfeed)
                    {
                        foreach (XmlNode childNode in node.ChildNodes)
                        {
                            if (childNode.InnerText != "")
                            {
                                for (int i = dict.Count - 1; i >= 0; i--)
                                {
                                    if (childNode.InnerText == dict.ElementAt(i).Key.ToString())
                                    {
                                        string str = dict.ElementAt(i).Key.ToString();

                                        "xml." + str = dict.ElementAt(i).Key.ToString();
                                    }
                                }
                            }
                        }
                        xml.CreateXML(dirname, documentName);
                    }
                }

Open in new window


Thanks
Avatar of pothireddysunil
pothireddysunil

ASKER

Hi Anyone has any ideas for this request

I have a class and it has properties defined

I don't want to use class.property to assign values.

instead i want to build class instance + some string value to build the class.property  and assign values to it.

This is my class instance  -- AIXML xml = new AIXML();

it has properties like Library, Application etc.

i don't want to call them directly like xml.Library or xml.Application  etc., instead i want build them dynamically in a loop and assign values like

string str = dict.ElementAt(i).Key.ToString();

"xml." + str = dict.ElementAt(i).Value.ToString();

is this possible. Anyone has done or any ideas


like
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why that dynamic approach? Is your XML not stable?
Thanks i followed the same approcah and resolved it. Thanks, Sunil P