?
Solved

Add XML to asp.net Dictionary

Posted on 2010-08-21
3
Medium Priority
?
385 Views
Last Modified: 2012-05-10
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
Comment
Question by:cjJosephj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 12

Expert Comment

by:UnexplainedWays
ID: 33493681
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
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 33493768
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
 

Author Comment

by:cjJosephj
ID: 33494712
Thanks Guys for the replies. I ended up using Nmarun solution.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 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