学习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........ 有没有函数,或者有更好的方法。呵呵
必须要遍历的,还有你的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;
}
}
}
}
}
你只有通过dest_folder_src_plugin找到location="${dest_folder_src}\plugin",分离出来${dest_folder_src}后再找到dest_folder_src所在节点的location="${dest_folder_Backup}\src",依次找到最后没有包含$的location,然后把值全替换好。
使用xpath, XmlNode node=doc.SelectSingleNode("/a/property[name="dest_folder_src_plugin"]");
请见下面的代码,已经测试过,如果需要可稍做改动:
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;
}