1
[Serializable]
public struct Item
{
    public DateTime date;
    public string title;
    public string description;
}

Rss link
This is my existing structure. I create a collection of this structure which reads data from a rss XML(mentioned above). The Collection having the values according to the tag name means RSS Item tag has date,title,description and author etc. Each item object having item (date) value of item tag's (date) value. But in the mentioned rss there is no date tag so my date value get blank value this is ok.But In our application user set which tag should use it may be a custom tag.Now I want to create a structure whose variable will create at run time with string datatype.

madan
  • 773
  • 13
  • 38
  • Why do you care what the variable is called? Why not just have a string variable and when you serialize it to XML, use the appropriate element name at that point? (I'd also suggest *not* having public fields or mutable structs, but that's a different matter.) I suspect the problem here is that you want the serialization to happen automatically, but it sounds like you'll need to do it manually in order to get the custom element names. LINQ to XML makes it pretty simple though. – Jon Skeet Dec 24 '14 at 09:10
  • maybe a duplicate of http://stackoverflow.com/questions/3862226/dynamically-create-a-class-in-c-sharp – Mitja Dec 24 '14 at 09:13
  • @JonSkeet thank the Idea but a rss contain fo large number of tags.I don't think quarrying for each input is feasible. – madan Dec 24 '14 at 09:37
  • Not sure what you mean by "quarrying", but reading and writing RSS explicitly is entirely feasible. – Jon Skeet Dec 24 '14 at 09:42

2 Answers2

1

By using following code I resolve my problem.

public class DynamicDictionary
{
    private IDictionary<string, string> dictionaryKeyValuePair;

    public DynamicDictionary()
    {
        dictionaryKeyValuePair = new Dictionary<string, string>();
    }
    public void AddKeyValuePair(string key, string value)
    {
        dictionaryKeyValuePair.Add(key, value);
    }
    public string GetValueByKeyName(string Key)
    {
        string value = "";
        foreach (var keyValuePair in dictionaryKeyValuePair)
        {
            if (keyValuePair.Key.Equals(Key, StringComparison.CurrentCultureIgnoreCase))
            {
                return keyValuePair.Value;
            }
        }
        return value;
    }
}

Assigning Value to the list

private Collection<string> tickerAtributesList=new Collection<string>();
tickerAtributesList.add("date");
tickerAtributesList.add("title");
tickerAtributesList.add("description");

XmlNodeList nodes = xmlDoc.SelectNodes("rss/channel/item");
keyValuePair = new Collection<DynamicDictionary>();
        foreach (XmlNode node in nodes)
        {
            DynamicDictionary tempDynamicDictionary = new DynamicDictionary();
            foreach (string tickerAtribute in tickerAtributesList)
            {
                tempDynamicDictionary.AddKeyValuePair(tickerAtribute, ParseDocElements(node, tickerAtribute));
            }
            keyValuePair.Add(tempDynamicDictionary);
        }

and the following is how to use

foreach (var item in keyValuePair)
{
    foreach (string tickerAtribute in tickerAtributesList)
    {
        string tempAtributeVal = item.GetValueByKeyName(tickerAtribute);
        //my code
    }
}
madan
  • 773
  • 13
  • 38
0

You do not need a new runtime structure with dynamic [string] variables because XML is already the struct you need.

string rssXml = "<xml>...</xml>";
XDocument doc = XDocument.Parse(rssXml);
string date = doc.Root.Element("date");
string title = doc.Root.Element("title");
string dynamicVar = doc.Root.Element("dynamicVar");

Even if you implement dynamic object building, nothing will be changed in your business logic.

EDIT

<xml>
    <books>
        <book id="1">FirstBook</book>
        <book id="2">SecondBook</book>
    </books>
</xml>

XDocument doc = XDocument.Parse(xml);
foreach(XElement el in doc.Root.Element("books").Elements("book"))
{
    string bookId = el.Attribute("id").Value;
    string bookName = el.Value;
}
opewix
  • 4,993
  • 1
  • 20
  • 42