如何从XML读取属性值并使用C#将其存储在List中 [英] How to read attribute values from XML and store it in List using C#

查看:579
本文介绍了如何从XML读取属性值并使用C#将其存储在List中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含xml值的XML文件。



我想将这些值提取到List< double>因此我可以操作以供进一步使用。



如何有效地提取这些值并将它们存储在List< double>



XML:

 <  形状 >  
< Point <跨度类=代码属性> X <跨度类=代码关键字> = <跨度类=代码关键字> 13650 y = 12000 z = <跨度类= 代码关键字 > - 500 <跨度类= 代码属性> <跨度类= code-keyword> / >
< Point x = 13653.6115 y = 11926.4871< /跨度> <跨度类= 代码属性> <跨度类= 代码属性>ž <跨度类= 代码关键字> = <跨度类= 代码关键字> - 500 <跨度类= 代码属性 > <跨度类= 代码关键字 > / <跨度类= 代码-keyword>>
< Point x = 13664.411 y = 11853.6823 z = - 500 / >
< < span class =code-leadattribute> Point
x = 13682.2947 y = 11782.2865 z = - 500 / >
< Point x = 13707.0904 < span class =code-attribute> y = 11712.9874 z = <跨度类= 代码关键字 > - 500 <跨度类= 代码属性> <跨度类= 代码关键字> / >
< Point x = 13738.5591 <跨度类= 代码属性 > <跨度类= 代码属性>ý <跨度CLAS s =code-keyword> = 11646.4524 z = - 500 / >
< /形状 >







列表< double> x =新列表< double>();

列表< double> y =新列表< double>();

列表< double> z = new List< double>();



如何在列表中添加这些属性值

解决方案

< blockquote>查看以下示例:

使用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;


这篇关于如何从XML读取属性值并使用C#将其存储在List中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆