且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从XML读取属性值并使用C#将其存储在List中

更新时间:2023-11-14 15:23:58

查看以下示例:

使用System; 
使用System.Collections.Generic;
使用System.Windows.Forms;
使用System.Xml;
使用System.IO;

命名空间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]);
}
}
}
}



此解决方案将读取xml文件中的x值并将其存储在一个名为listx的列表。我将留给你为y和z做同样的事。


我已经测试过它的工作原理



  private   void  Form1_Load( object  sender,EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load( @ C:\ Users \admin \Desktop\prova2 \ file.xml跨度>); // 在此处插入文件路径

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节点
{
x.Add(node.Attributes [ x]。Value);
y.Add(node.Attributes [ y]。Value);
z.Add(node.Attributes [ z]。Value);
}
}





记得申报



使用System.Xml; 


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:

<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

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.


I have tested and it works

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;