Click here to Skip to main content
15,912,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having an XML file which holds xml values.

I want to extract these values to a List<double> so that I can manipulate for further use.

How to efficiently extract these values and store them in List<double>

XML:
XML
<Shapes>
  <Point x="13650" y="12000" z="-500" />
  <Point  x="13653.6115" y="11926.4871" z="-500" />
  <Point x="13664.411" y="11853.6823" z="-500" />
  <Point x="13682.2947" y="11782.2865" z="-500" />
  <Point  x="13707.0904" y="11712.9874" z="-500" />
  <Point x="13738.5591" y="11646.4524" z="-500" />
</Shapes>



List<double> x=new List<double>();
List<double> y=new List<double>();
List<double> z=new List<double>();

How to add these attribute values in the list
Posted

Check out the following example:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace WindowsFormsApplicationCS
{
    public partial class Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlNodeList xmlnode;
            int i = 0;
            List<string> listx = new List<string>();
      
            FileStream fs = new FileStream("XMLFILE1.xml", FileMode.Open, FileAccess.Read);
            xmldoc.Load(fs);
            xmlnode = xmldoc.GetElementsByTagName("Point");
            for (i = 0; i <= xmlnode.Count - 1; i++)
            {
                listx.Add(xmlnode[i].Attributes["x"].Value);
            }

            for (int k = 0; k < listx.Count; k++)
	        {
                MessageBox.Show(listx[k]);
	        }
        }
    }
}

This solution will read the x values from the xml file and store them in a list called listx. I shall leave it to you to do the same for y and z.
 
Share this answer
 
I have tested and it works

C#
private void Form1_Load(object sender, EventArgs e)
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(@"C:\Users\admin\Desktop\prova2\file.xml"); //insert the file path here

           XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Shapes/Point");

           List<string> x = new List<string>();
           List<string> y = new List<string>();
           List<string> z = new List<string>();

           foreach (XmlNode node in nodes)
           {
               x.Add(node.Attributes["x"].Value);
               y.Add(node.Attributes["y"].Value);
               z.Add(node.Attributes["z"].Value);
           }
       }


remember to declare

using System.Xml;
 
Share this answer
 
v2
Comments
Member 14815583 28-May-20 6:03am    
hey, its a very old link but if any of the node value is 0, then xml value is returning NULL and hence causing exceptions. How to fix it.
Freedom-96 5-Jan-21 4:01am    
you can add 'if (node.Attributes["x"] != null)' above the 'x.Add(node.Attributes["x"].Value)' and so on

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