Solved

Create a class property using a variable.

Posted on 2014-12-16
5
86 Views
Last Modified: 2015-01-12
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
0
Comment
Question by:pothireddysunil
  • 2
5 Comments
 
LVL 1

Author Comment

by:pothireddysunil
ID: 40503391
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
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 40504192
I think you want to use Reflection to do this.  That allows you to call methods based on their names as Strings.  Which is essentially what you're doing here.

I'm not very familiar with the details for doing this in C#, but I think the method you're looking for is SetValue:
http://msdn.microsoft.com/en-us/library/xb5dd1f1%28v=vs.110%29.aspx

So something like this sequence:

// Get the "str" property of xml object (i.e. "xml." + str)
PropertyInfo propertyInfo = xml.GetType().GetProperty(str);

// Set a new value for this property
propertyInfo.SetValue(xml, dict.ElementAt(i).Value.ToString(), null);

should achieve what you're trying to do with
"xml." + str = dict.ElementAt(i).Value.ToString();

Hope that helps,

Doug
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40504296
Why that dynamic approach? Is your XML not stable?
0
 
LVL 1

Author Closing Comment

by:pothireddysunil
ID: 40545889
Thanks i followed the same approcah and resolved it. Thanks, Sunil P
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help on C# Linq query on object 6 62
Close form "before" open 3 40
How useful is the free version of Selenium? 3 31
EF5: Question about Metadata Artifact Processing 4 15
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now