Click here to Skip to main content
15,885,887 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I have the follwing xml file:

<Directorys>
  <DIR>
    <name>Installation Path</name>
    <path>C:\Users\briganti\Documents\Lavoro\PIPPO</path>
  </DIR>
  <DIR>
    <name>Temp directory</name>
    <path>C:\Users\briganti\Documents\Lavoro\PLUTO</path>
  </DIR>
  <DIR>
    <name>Test List Path</name>
    <path>C:\Users\briganti\Documents\Lavoro\PAPERINO</path>
  </DIR>
</Directorys>


and I want to read and store the three path value in three different string variables using C# language. I used the following code and it works but is it possible to do the same thing more simply?

What I have tried:

XmlDocument doc = new XmlDocument();
            doc.Load(Directory.GetCurrentDirectory() + @"\programconfig.xml");
            XmlNodeList paths = doc.DocumentElement.SelectNodes("/Directorys/DIR/path");
            var myList = new List<string>();
            foreach (XmlNode path in paths)
            {
                // Create list
                myList.Add(path.InnerText);
                //MessageBox.Show(path.InnerText);

            }
            // Convert to array
            var myArray = myList.ToArray();
            t_Installazione.Text = myArray[0];
            t_Temp.Text = myArray[1];
            t_testlist.Text = myArray[2];
Posted
Updated 21-Apr-20 8:23am
v2
Comments
CHill60 21-Apr-20 9:32am    
You need to show us the code you have tried and tell us what the problem is

I'm feeling generous - must be an ISO/Alcohol thing .... this might start you off

XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

foreach(XmlNode row in doc.SelectNodes("//DIR"))
{
   string rowName = row.SelectSingleNode("//path").InnerText;
   Console.WriteLine("{0}", rowName);
}


Another approach might be to deserialize the XML to a list of Objects, eg with name & path attributes Convert XML to C# Object[^]
 
Share this answer
 
You can also use XElement, which is a bit lighter than using XmlDocument:
//using System.Linq;
//using System.Xml.Linq;

XElement x = XElement.Load("programconfig.xml");

foreach (var item in x.Elements())
{
    Debug.Print(item.Element("path").Value.ToString());
}
 
Share this answer
 
You might want to consider what happens if the DIR elements are not in the expected order, or any of the paths are missing.
C#
string filePath = Path.GetFullPath(@".\programconfig.xml");
XElement doc = XElement.Load(filePath);

Dictionary<string, string> paths = doc.Descendants("DIR").ToDictionary(
    d => (string)d.Element("name"), 
    d => (string)d.Element("path"), 
    StringComparer.OrdinalIgnoreCase);

string value;
if (paths.TryGetValue("Installation Path", out value))
{
    t_Installazione.Text = value;
}
if (paths.TryGetValue("Temp directory", out value))
{
    t_Temp.Text = value;
}
if (paths.TryGetValue("Test List Path", out value))
{
    t_testlist.Text = value;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900