首页 新闻 会员 周边

请问如何把某个文件的路径写到XML中

0
悬赏园豆:10 [待解决问题]

需求如下:
现在我的界面上有10个按钮,这10个按钮分别是打开不同的应用程序
由于我程序安装的位置都是不统一的,所有需要用户自己去找下 然后配置进XML中。
求一个DEMO

C#语言

清风小子的主页 清风小子 | 初学一级 | 园豆:164
提问于:2016-10-22 14:33
< >
分享
所有回答(1)
0

1、你这个只是读写xml的代码,百度一搜一大片。

2、你求DEMO,然而却没说是什么语言。

 

雨之秋水 | 园豆:649 (小虾三级) | 2016-10-22 15:08

人不说了c#语言吗?

支持(0) 反对(0) ~扎克伯格 | 园豆:1923 (小虾三级) | 2016-10-23 08:03

@IT民工-杰: 那是我回复之后修改的好么!!!

支持(0) 反对(0) 雨之秋水 | 园豆:649 (小虾三级) | 2016-10-28 09:16
  1 public class XmlHelper
  2     {
  3         #region 增、删、改操作   
  4         /// <summary>
  5         /// 追加节点
  6         /// </summary>
  7         /// <param name="filePath">XML文档绝对路径</param>
  8         /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
  9         /// <param name="xmlNode">XmlNode节点</param>
 10         /// <returns></returns>
 11         public static bool AppendChild(string filePath, string xPath, XmlNode xmlNode)
 12         {
 13             try
 14             {
 15                 XmlDocument doc = new XmlDocument();
 16                 doc.Load(filePath);
 17                 XmlNode xn = doc.SelectSingleNode(xPath);
 18                 XmlNode n = doc.ImportNode(xmlNode, true);
 19                 xn.AppendChild(n);
 20                 doc.Save(filePath);
 21                 return true;
 22             }
 23             catch
 24             {
 25                 return false;
 26             }
 27         }
 28 
 29         /// <summary>
 30         /// 从XML文档中读取节点追加到另一个XML文档中
 31         /// </summary>
 32         /// <param name="filePath">需要读取的XML文档绝对路径</param>
 33         /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
 34         /// <param name="toFilePath">被追加节点的XML文档绝对路径</param>
 35         /// <param name="toXPath">范例: @"Skill/First/SkillItem"</param>
 36         /// <returns></returns>
 37         public static bool AppendChild(string filePath, string xPath, string toFilePath, string toXPath)
 38         {
 39             try
 40             {
 41                 XmlDocument doc = new XmlDocument();
 42                 doc.Load(toFilePath);
 43                 XmlNode xn = doc.SelectSingleNode(toXPath);
 44 
 45                 XmlNodeList xnList = ReadNodes(filePath, xPath);
 46                 if (xnList != null)
 47                 {
 48                     foreach (XmlElement xe in xnList)
 49                     {
 50                         XmlNode n = doc.ImportNode(xe, true);
 51                         xn.AppendChild(n);
 52                     }
 53                     doc.Save(toFilePath);
 54                 }
 55                 return true;
 56             }
 57             catch
 58             {
 59                 return false;
 60             }
 61         }
 62 
 63         /// <summary>
 64         /// 修改节点的InnerText的值
 65         /// </summary>
 66         /// <param name="filePath">XML文件绝对路径</param>
 67         /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
 68         /// <param name="value">节点的值</param>
 69         /// <returns></returns>
 70         public static bool UpdateNodeInnerText(string filePath, string xPath, string value)
 71         {
 72             try
 73             {
 74                 XmlDocument doc = new XmlDocument();
 75                 doc.Load(filePath);
 76                 XmlNode xn = doc.SelectSingleNode(xPath);
 77                 XmlElement xe = (XmlElement)xn;
 78                 xe.InnerText = value;
 79                 doc.Save(filePath);
 80             }
 81             catch
 82             {
 83                 return false;
 84             }
 85             return true;
 86         }
 87 
 88         /// <summary>
 89         /// 读取XML文档
 90         /// </summary>
 91         /// <param name="filePath">XML文件绝对路径</param>
 92         /// <returns></returns>
 93         public static XmlDocument LoadXmlDoc(string filePath)
 94         {
 95             try
 96             {
 97                 XmlDocument doc = new XmlDocument();
 98                 doc.Load(filePath);
 99                 return doc;
100             }
101             catch
102             {
103                 return null;
104             }
105         }
106         #endregion 增、删、改操作
107 
108         #region 扩展方法
109         /// <summary>
110         /// 读取XML的所有子节点
111         /// </summary>
112         /// <param name="filePath">XML文件绝对路径</param>
113         /// <param name="xPath">范例: @"Skill/First/SkillItem"</param>
114         /// <returns></returns>
115         public static XmlNodeList ReadNodes(string filePath, string xPath)
116         {
117             try
118             {
119                 XmlDocument doc = new XmlDocument();
120                 doc.Load(filePath);
121                 XmlNode xn = doc.SelectSingleNode(xPath);
122                 XmlNodeList xnList = xn.ChildNodes;  //得到该节点的子节点
123                 return xnList;
124             }
125             catch
126             {
127                 return null;
128             }
129         }
130 
131         #endregion 扩展方法
132     }

 

支持(0) 反对(0) 雨之秋水 | 园豆:649 (小虾三级) | 2016-10-28 09:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册