首页新闻找找看学习计划

C# 读取xml节点值

0
悬赏园豆:30 [已解决问题] 解决于 2011-12-30 10:04

学习C#不久啊,请各位大虾帮忙看下:

<property name="version" value="myProject" />
<!-- PackageFolder value值需要修改(打包备份的路径) -->
<property name="PackageFolder" value="G:\AutoBuild\打包记录" />
<property name="dest_folder_Backup" location="${PackageFolder}/${version}/${myDate}" />
<property name="dest_folder_include" location="${dest_folder_Backup}\include" />
<property name="dest_folder_src" location="${dest_folder_Backup}\src" />
<property name="dest_folder_src_plugin" location="${dest_folder_src}\plugin" />

 

有没有方法取得dest_folder_src_plugin的值?

我只取到location=“${dest_folder_src}\\plugin”, 不会需要再次遍历XML找到dest_folder_src,找到dest_folder_src后再找dest_folder_Backup........ 有没有函数,或者有更好的方法。呵呵

FiendReam的主页 FiendReam | 初学一级 | 园豆:8
提问于:2011-12-27 16:11
< >
分享
最佳答案
0

必须要遍历的,还有你的xml文件没有根元素,以下是我写的代码,经过调试的,你看着改改..

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;
using System.Text.RegularExpressions;

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

        private void button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"F:\a.xml");
            XmlNodeList nodes=doc.SelectNodes("/a/property");
            string temp = "";
            Regex regex = new Regex(@"{.*}");
            foreach(XmlNode i in nodes)
            {
             if("dest_folder_src_plugin"==i.Attributes["name"].Value)
             {
                 temp =regex.Match(i.Attributes["location"].Value).Value;
                 temp = temp.Substring(1,temp.Length-2);
             }
            }
            foreach (XmlNode i in nodes)
            {
                if (temp== i.Attributes["name"].Value)
                {
                    temp = regex.Match(i.Attributes["location"].Value).Value;
                    richTextBox1.Text = temp.Substring(1,temp.Length-2);
                    return;
                }
            }
            
        }
    }
}

收获园豆:15
hexllo | 菜鸟二级 |园豆:405 | 2011-12-27 17:13
其他回答(3)
0

你只有通过dest_folder_src_plugin找到location="${dest_folder_src}\plugin",分离出来${dest_folder_src}后再找到dest_folder_src所在节点的location="${dest_folder_Backup}\src",依次找到最后没有包含$的location,然后把值全替换好。

收获园豆:3
LCM | 园豆:6876 (大侠五级) | 2011-12-27 16:20
0

使用xpath, XmlNode node=doc.SelectSingleNode("/a/property[name="dest_folder_src_plugin"]");

http://msdn.microsoft.com/zh-cn/library/ms256086.aspx

收获园豆:2
碧落星痕 | 园豆:708 (小虾三级) | 2011-12-28 11:34
1

请见下面的代码,已经测试过,如果需要可稍做改动:

        static void test560()
{
// 为你的XML内容加上根节点root
string str = @"<root><property name=""version"" value=""myProject"" />
<!-- PackageFolder value值需要修改(打包备份的路径) -->
<property name=""PackageFolder"" value=""G:\AutoBuild\打包记录"" />
<property name=""dest_folder_Backup"" location=""${PackageFolder}/${version}/${myDate}"" />
<property name=""dest_folder_include"" location=""${dest_folder_Backup}\include"" />
<property name=""dest_folder_src"" location=""${dest_folder_Backup}\src"" />
<property name=""dest_folder_src_plugin"" location=""${dest_folder_src}\plugin"" /></root>
";

// 创建XElement
XElement root = XElement.Parse(str);

// 调用PraseLocation方法来解析location
string location = ParseLocation(root, "dest_folder_src_plugin");

Console.WriteLine(location);
}

// 解析location的方法
private static string ParseLocation(XElement root, string name)
{
// 获取 location (Linq to XML)
string location = root.Elements()
.Where(e => e.Attribute("name").Value == name)
.Select(e => e.Attributes().Any(a => a.Name == "location") ? e.Attribute("location").Value : e.Attribute("value").Value).FirstOrDefault();

if (location != null)
{
// 正则表达式匹配路径中的变量
Match m = Regex.Match(location, @"\${.*?}");
while (m.Success)
{
// 递归调用ParseLocation来替换变量
location = location.Replace(m.Value, ParseLocation(root, m.Value.Substring(2, m.Value.Length - 3)));
m = Regex.Match(location, @"\${.*?}");
}
}
return location;
}
收获园豆:10
Life a Poem | 园豆:437 (菜鸟二级) | 2011-12-29 11:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册