c#读取xml中特定节点的值(实例)

简介: (转载请注明出处:http://blog.csdn.net/buptgshengod) 将要读取的是smoke下floortype中的value值,xml如下 <?xml version="1.0" encoding="UTF-8" ?> - <PropDataBucket name="navisworks_df_floor_cicle" versi

(转载请注明出处:http://blog.csdn.net/buptgshengod


将要读取的是smoke下floortype中的value值,xml如下
  <?xml version="1.0" encoding="UTF-8" ?> 
- <PropDataBucket name="navisworks_df_floor_cicle" version="1.0">
- <PropDataRow name="smoke" index="0" type="custom">
- <target name="properties">
  <property name="floortype" value="circular" /> 
  <property name="length" value="" /> 
  </target>
  </PropDataRow>
- <PropDataRow name="navisworks_df_floor_rectangular_01" index="1" type="">
- <target name="properties">
  <property name="floortype" value="rectangular" /> 
  <property name="length" value="1000.00" /> 
  </target>
  </PropDataRow>
  </PropDataBucket>

c#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            string str = "";
            XmlDocument doc = new XmlDocument();
            doc.Load(@"e:\test.xml");//读入xml,注意@
            XmlNode node = doc.SelectSingleNode("//PropDataBucket/PropDataRow[@name='smoke']//property[@name='floortype']");//设置节点位置
            if (node != null)
            {
             
                str = node.Attributes["value"].Value;//节点下多个数值名称的选择
            }
          
            textBox1.Text = str;
          
        }
    }
}

效果如图

目录
相关文章
|
22天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
2天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
8天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
5天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
14天前
|
XML 分布式计算 资源调度
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(一)
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(一)
82 5
|
13天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
14天前
|
XML 资源调度 网络协议
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(二)
大数据-02-Hadoop集群 XML配置 超详细 core-site.xml hdfs-site.xml 3节点云服务器 2C4G HDFS Yarn MapRedece(二)
38 4
|
14天前
|
分布式计算 资源调度 Hadoop
大数据-01-基础环境搭建 超详细 Hadoop Java 环境变量 3节点云服务器 2C4G XML 集群配置 HDFS Yarn MapRedece
大数据-01-基础环境搭建 超详细 Hadoop Java 环境变量 3节点云服务器 2C4G XML 集群配置 HDFS Yarn MapRedece
45 4
|
23天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树
|
7天前
|
XML JavaScript 数据格式
XML DOM 遍历节点树
XML DOM 遍历节点树