• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Add XML to asp.net Dictionary

Hi,

I have the following xml file


<fields>
<field>
      <db value=@name/>
      <ui value=txtName/>
</field>
<field>
      <db value=@name2 />
      <ui value=txtName2 />
</field>

<field>
      <db value=@name3 />
      <ui value=txtName 3 />
</field>

</fields>

I'm trying to achieve this
                Map.Add("@name ", " txtName");
                Mapr.Add("@name2", " txtName2");
                Map.Add("@name3", " txtName3");


Thanks in advance

I have been trying to add all the values to a  dictionary. Not sure why but my data from my xml file does not get added t the dictionary.

   private static void MapFile()
        {
	           Map = new Dictionary<string, string>();
            Map.Clear();

                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(@"C:\XML\XMLFILE.xml");

             XmlNode commandnode = xdoc.DocumentElement.SelectSingleNode("/root/fields/field");
                Dictionary<string, string> Map = new Dictionary<string, string>();
                 {
                    foreach (XmlNode inneritem in commandnode)
                    {
                        foreach (XmlNode data in inneritem)
                        {
                            Map.Add(????, ????);
                        }
                    }
                
                
                 }
               }

Open in new window

0
cjJosephj
Asked:
cjJosephj
1 Solution
 
UnexplainedWaysCommented:
Code can be cleaned up a bit but you get the idea
XElement root = XElement.Load(MapPath("~/XMLFile.xml"));
        foreach (XElement field in root.Elements())
        {
            string db = field.Element("db").Attribute("value").Value;
            string ui = field.Element("ui").Attribute("value").Value;

            Map(db, ui);
        }
        
    }

    public void Map(string db, string ui)
    {
        Response.Write(string.Format("{0} = {1} <hr/>", db, ui));
    }

Open in new window

0
 
nmarunCommented:
Try something like this.Arun
private static void ReplaceDictionaryElements()
{
    // create a collection of what you need to replace
    Dictionary<string, string> dictionary = new Dictionary<string, string>
                                                {
                                                    {"@name", " txtName"},
                                                    {"@name2", " txtName2"},
                                                    {"@name3", " txtName3"}
                                                };
    // load a the Xml
    XDocument xDocument = XDocument.Load(DataXmlFilePath);

    // write a LINQ statement that checks the <db> element's value attribute
    // with the dictionary's key
    // and creates a new collection of IEnumerable type 
    var replacedDictionary = from field in xDocument.Descendants("field")
                             from entry in dictionary
                             where field.Element("db").Attribute("value").Value == entry.Key
                             select new
                                        {
                                            Key = entry.Value,
                                            Value = field.Element("ui").Attribute("value").Value
                                        };
    // loop through the collection            
    foreach (var entry in replacedDictionary)
    {
        Console.WriteLine("{0}-{1}", entry.Key, entry.Value);
    }
}

Open in new window

0
 
cjJosephjAuthor Commented:
Thanks Guys for the replies. I ended up using Nmarun solution.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now