XMLoperate.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. using System.Xml;
  8. namespace OTSCommon
  9. {
  10. public class XMLoperate
  11. {
  12. #region 读取XML到DataSet
  13. /// <summary>
  14. /// 功能:读取XML到DataSet中
  15. /// </summary>
  16. /// <param name="XmlPath">xml路径</param>
  17. /// <returns>DataSet</returns>
  18. public static DataSet GetXml(string XmlPath)
  19. {
  20. DataSet ds = new DataSet();
  21. ds.ReadXml(@XmlPath);
  22. return ds;
  23. }
  24. #endregion
  25. #region 读取xml文档并返回一个节点
  26. /// <summary>
  27. /// 读取xml文档并返回一个节点:适用于一级节点
  28. /// </summary>
  29. /// <param name="XmlPath">xml路径</param>
  30. /// <param name="NodeName">节点</param>
  31. /// <returns></returns>
  32. public static string ReadXmlReturnNode(string XmlPath, string Node)
  33. {
  34. XmlDocument docXml = new XmlDocument();
  35. docXml.Load(@XmlPath);
  36. XmlNodeList xn = docXml.GetElementsByTagName(Node);
  37. return xn.Item(0).InnerText.ToString();
  38. }
  39. #endregion
  40. #region 查找数据,返回一个DataSet
  41. /// <summary>
  42. /// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
  43. /// </summary>
  44. /// <param name="xmlPath">xml文档路径</param>
  45. /// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
  46. /// <returns></returns>
  47. public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
  48. {
  49. XmlDocument objXmlDoc = new XmlDocument();
  50. objXmlDoc.Load(xmlPath);
  51. DataSet ds = new DataSet();
  52. StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
  53. ds.ReadXml(read);
  54. return ds;
  55. }
  56. #endregion
  57. //必须创建对象才能使用的类
  58. private bool _alreadyDispose = false;
  59. private XmlDocument xmlDoc = new XmlDocument();
  60. private string _xmlPath;
  61. #region 构造与释构
  62. public XMLoperate()
  63. {
  64. }
  65. public XMLoperate(string xmlPath)
  66. {
  67. _xmlPath = xmlPath;
  68. }
  69. ~XMLoperate()
  70. {
  71. Dispose();
  72. }
  73. protected virtual void Dispose(bool isDisposing)
  74. {
  75. if (_alreadyDispose) return;
  76. if (isDisposing)
  77. {
  78. //
  79. }
  80. _alreadyDispose = true;
  81. }
  82. #endregion
  83. #region IDisposable 成员
  84. public void Dispose()
  85. {
  86. Dispose(true);
  87. GC.SuppressFinalize(this);
  88. }
  89. #endregion
  90. #region 根据父节点属性值读取子节点值
  91. /// <summary>
  92. /// 根据父节点属性读取字节点值
  93. /// </summary>
  94. /// <param name="XmlPath">xml路径</param>
  95. /// <param name="FatherElenetName">父节点名</param>
  96. /// <param name="AttributeName">属性值</param>
  97. /// <param name="AttributeIndex">属性索引</param>
  98. /// <param name="ArrayLength">要返回的节点数组长度</param>
  99. /// <returns></returns>
  100. public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength)
  101. {
  102. System.Collections.ArrayList al = new System.Collections.ArrayList();
  103. XmlDocument docXml = new XmlDocument();
  104. docXml.Load(@XmlPath);
  105. XmlNodeList xn = docXml.DocumentElement.ChildNodes;
  106. //遍历第一层节点
  107. foreach (XmlElement element in xn)
  108. {
  109. //判断父节点是否为指定节点
  110. if (element.Name == FatherElenetName)
  111. {
  112. //判断父节点属性的索引是否大于指定索引
  113. if (element.Attributes.Count < AttributeIndex)
  114. return null;
  115. //判断父节点的属性值是否等于指定属性
  116. if (element.Attributes[AttributeIndex].Value == AttributeName)
  117. {
  118. XmlNodeList xx = element.ChildNodes;
  119. if (xx.Count > 0)
  120. {
  121. for (int i = 0; i < ArrayLength & i < xx.Count; i++)
  122. {
  123. al.Add(xx[i].InnerText);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. return al;
  130. }
  131. #endregion
  132. #region 根据节点属性读取子节点值(较省资源模式)
  133. /// <summary>
  134. /// 根据节点属性读取子节点值(较省资源模式)
  135. /// </summary>
  136. /// <param name="XmlPath">xml路径</param>
  137. /// <param name="FatherElement">父节点值</param>
  138. /// <param name="AttributeName">属性名称</param>
  139. /// <param name="AttributeValue">属性值</param>
  140. /// <param name="ArrayLength">返回的数组长度</param>
  141. /// <returns></returns>
  142. public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
  143. {
  144. System.Collections.ArrayList al = new System.Collections.ArrayList();
  145. XmlDocument docXml = new XmlDocument();
  146. docXml.Load(@XmlPath);
  147. XmlNodeList xn;
  148. xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
  149. XmlNodeList xx = xn.Item(0).ChildNodes;
  150. for (int i = 0; i < ArrayLength & i < xx.Count; i++)
  151. {
  152. al.Add(xx.Item(i).InnerText);
  153. }
  154. return al;
  155. }
  156. #endregion
  157. #region 根据父节点属性值读取子节点值
  158. /// <summary>
  159. /// 根据父节点属性读取字节点值
  160. /// </summary>
  161. /// <param name="XmlPath">xml路径</param>
  162. /// <returns></returns>
  163. public static Dictionary<string, object> GetXMLAllInfo(string XmlPath)
  164. {
  165. if (XmlPath == "")
  166. {
  167. return null;
  168. }
  169. Dictionary<string, object> suggestions = new Dictionary<string, object>();
  170. XmlDocument docXml = new XmlDocument();
  171. docXml.Load(@XmlPath);
  172. XmlNodeList xn = docXml.DocumentElement.ChildNodes;
  173. //遍历第一层节点
  174. foreach (XmlElement element in xn)
  175. {
  176. string name = element.Name;
  177. if ("Collection"!= name)
  178. {
  179. int attributes = element.Attributes.Count;
  180. Dictionary<string, object> obj = new Dictionary<string, object>();
  181. string key = "";
  182. foreach (System.Xml.XmlAttribute item in element.Attributes)
  183. {
  184. if (item.Name == "RegName")
  185. {
  186. key = item.Value;
  187. }
  188. else
  189. {
  190. obj.Add(item.Name, item.Value);
  191. }
  192. }
  193. if (element.ChildNodes.Count > 0)
  194. {
  195. Dictionary<string, object> childList = GetChildInfo(element.ChildNodes);
  196. if (childList.Count > 0)
  197. {
  198. obj.Add("Members", childList);
  199. }
  200. }
  201. suggestions.Add(key, obj);
  202. }
  203. }
  204. return suggestions;
  205. }
  206. private static Dictionary<string, object> GetChildInfo(XmlNodeList childs)
  207. {
  208. Dictionary<string, object> child = new Dictionary<string, object>();
  209. foreach (XmlElement element in childs)
  210. {
  211. if (element.Name != "Member")
  212. continue;
  213. int attributes = element.Attributes.Count;
  214. Dictionary<string, object> obj = new Dictionary<string, object>();
  215. string key = "";
  216. foreach (System.Xml.XmlAttribute item in element.Attributes)
  217. {
  218. if (item.Name == "RegName")
  219. {
  220. key = item.Value;
  221. }
  222. else
  223. {
  224. obj.Add(item.Name, item.Value);
  225. }
  226. }
  227. if (element.ChildNodes.Count > 0)
  228. {
  229. Dictionary<string, object> childList = GetChildInfo(element.ChildNodes);
  230. if (childList.Count > 0)
  231. {
  232. obj.Add("Members", childList);
  233. }
  234. }
  235. child.Add(key, obj);
  236. }
  237. return child;
  238. }
  239. #endregion
  240. public static List<string> GetMember(string XmlPath, string tem)
  241. {
  242. if (XmlPath == "")
  243. {
  244. return null;
  245. }
  246. XmlDocument docXml = new XmlDocument();
  247. docXml.Load(@XmlPath);
  248. XmlNodeList xn = docXml.DocumentElement.ChildNodes[0].ChildNodes;
  249. List<string> elem = new List<string>();
  250. //遍历第一层节点
  251. foreach (XmlElement element in xn)
  252. {
  253. foreach (System.Xml.XmlAttribute item in element.Attributes)
  254. {
  255. if (item.Name == "TemplateName" && item.Value == tem)
  256. {
  257. elem.Add(element.ChildNodes[0].ChildNodes[0].Attributes["ElementName"].Value);
  258. elem.Add(element.ChildNodes[0].ChildNodes[1].Attributes["ElementName"].Value);
  259. elem.Add(element.ChildNodes[0].ChildNodes[2].Attributes["ElementName"].Value);
  260. return elem;
  261. }
  262. }
  263. }
  264. return elem;
  265. }
  266. /// <summary>
  267. /// 修改xml
  268. /// </summary>
  269. /// <param name="FilePath">文件地址</param>
  270. /// <param name="RegName">节点名称</param>
  271. /// <param name="Name">节点属性名称</param>
  272. /// <param name="Value">节点属性值</param>
  273. public static bool EditXmlInfo(string FilePath, string TagName, string Name, string Value)
  274. {
  275. try
  276. {
  277. XmlDocument xmlDoc = new XmlDocument();
  278. xmlDoc.Load(FilePath); //加载Xml文件
  279. XmlNodeList nodeList = xmlDoc.GetElementsByTagName(TagName);
  280. foreach (XmlNode node in nodeList)
  281. {
  282. XmlElement ele = (XmlElement)node;
  283. ele.SetAttribute(Name, Value);
  284. }
  285. xmlDoc.Save(FilePath);
  286. return true;
  287. }
  288. catch /*(Exception e)*/
  289. {
  290. return false;
  291. }
  292. }
  293. public static bool EditMemberXmlInfo(string FilePath, string TagName, string Name, string Value)
  294. {
  295. try
  296. {
  297. XmlDocument xmlDoc = new XmlDocument();
  298. xmlDoc.Load(FilePath); //加载Xml文件
  299. XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Member");
  300. foreach (XmlNode node in nodeList)
  301. {
  302. XmlElement ele = (XmlElement)node;
  303. if (ele.GetAttribute("RegName") == TagName)
  304. {
  305. ele.SetAttribute(Name, Value);
  306. }
  307. }
  308. xmlDoc.Save(FilePath);
  309. return true;
  310. }
  311. catch /*(Exception e)*/
  312. {
  313. return false;
  314. }
  315. }
  316. /// <summary>
  317. /// 获取XML节点参数
  318. /// </summary>
  319. /// <param name="Name">节点参数名称</param>
  320. /// <returns>节点参数</returns>
  321. public static string GetXMLInformations(string xmlFilePath, string Name)
  322. {
  323. try
  324. {
  325. string value = string.Empty;
  326. XmlDocument doc = new XmlDocument();
  327. doc.Load(xmlFilePath); //加载Xml文件
  328. XmlElement root = doc.DocumentElement; //获取根节点
  329. XmlNodeList mainNodes = root.GetElementsByTagName("parameter"); //获取子节点集合
  330. foreach (XmlNode node in mainNodes)
  331. {
  332. //获取Name属性值
  333. string name = ((XmlElement)node).GetAttribute("Name");
  334. if (name.Equals(Name))
  335. {
  336. value = ((XmlElement)node).GetAttribute("Value");
  337. }
  338. }
  339. return value;
  340. }
  341. catch (Exception)
  342. {
  343. return "";
  344. }
  345. }
  346. /// <summary>
  347. /// 修改第二层节点数据
  348. /// </summary>
  349. /// <param name="XmlPath">XML路径</param>
  350. /// <param name="AttributeName">所有需要修改的属性名称</param>
  351. /// <param name="Value">需要修改的值</param>
  352. /// <returns></returns>
  353. public static bool UpdateByAttribute(string XmlPath, string[] AttributeName, string[] Value)
  354. {
  355. try
  356. {
  357. XmlDocument docXml = new XmlDocument();
  358. docXml.Load(@XmlPath);
  359. XmlNodeList xn = docXml.DocumentElement.ChildNodes;
  360. //遍历第一层节点
  361. foreach (XmlElement element in xn)
  362. {
  363. if (element.Attributes[0].Value == Value[0])
  364. {
  365. for (int i = 0; i < element.Attributes.Count; i++)
  366. {
  367. for (int j = 0; j < AttributeName.Count(); j++)
  368. {
  369. //判断父节点的属性值是否等于指定属性
  370. if (element.Attributes[i].Name == AttributeName[j])
  371. {
  372. element.SetAttribute(AttributeName[j], Value[j]);
  373. break;
  374. }
  375. }
  376. }
  377. break;
  378. }
  379. }
  380. docXml.Save(@XmlPath);
  381. return true;
  382. }
  383. catch /*(Exception e)*/
  384. {
  385. return false;
  386. }
  387. }
  388. public static int InsertAttribute(string XmlPath, string[] AttributeName, string[] Value, string nodeName)
  389. {
  390. try
  391. {
  392. XmlDocument docXml = new XmlDocument();
  393. docXml.Load(@XmlPath);
  394. XmlNodeList xn = docXml.DocumentElement.ChildNodes;
  395. foreach (XmlElement element in xn)
  396. {
  397. if (element.Attributes[0].Value == Value[0])
  398. {
  399. return -1;
  400. }
  401. }
  402. XmlElement xe = docXml.CreateElement(nodeName);
  403. for (int i = 0; i < AttributeName.Count(); i++)
  404. {
  405. xe.SetAttribute(AttributeName[i], Value[i]);
  406. }
  407. docXml.DocumentElement.AppendChild(xe);
  408. docXml.Save(@XmlPath);
  409. return 1;
  410. }
  411. catch /*(Exception e)*/
  412. {
  413. return 0;
  414. }
  415. }
  416. public static int DeleteByAttribute(string XmlPath, string AttributeName, string Value)
  417. {
  418. try
  419. {
  420. XmlDocument docXml = new XmlDocument();
  421. docXml.Load(@XmlPath);
  422. XmlNodeList xn = docXml.DocumentElement.ChildNodes;
  423. foreach (XmlElement element in xn)
  424. {
  425. if (element.Attributes[AttributeName].Value == Value)
  426. {
  427. element.ParentNode.RemoveChild(element);
  428. docXml.Save(@XmlPath);
  429. return 1;
  430. }
  431. }
  432. return 2;
  433. }
  434. catch /*(Exception e)*/
  435. {
  436. return 0;
  437. }
  438. }
  439. #region 报告模板
  440. /// <summary>
  441. /// 写入配置,也可以用来修改
  442. /// </summary>
  443. /// <param name="value">写入的值</param>
  444. /// <param name="nodes">节点</param>
  445. public void Write(string value, params string[] nodes)
  446. {
  447. //初始化xml
  448. XmlDocument xmlDoc = new XmlDocument();
  449. if (File.Exists(_xmlPath))
  450. xmlDoc.Load(_xmlPath);
  451. else
  452. xmlDoc.LoadXml("<XmlConfig />");
  453. XmlNode xmlRoot = xmlDoc.ChildNodes[0];
  454. //新增、编辑 节点
  455. string xpath = string.Join("/", nodes);
  456. XmlNode node = xmlDoc.SelectSingleNode(xpath);
  457. if (node == null) //新增节点
  458. {
  459. node = makeXPath(xmlDoc, xmlRoot, xpath);
  460. }
  461. node.InnerText = value;
  462. //保存
  463. xmlDoc.Save(_xmlPath);
  464. }
  465. /// <summary>
  466. /// 设置节点上属性的值
  467. /// </summary>
  468. /// <param name="AttributeName">属性名</param>
  469. /// <param name="AttributeValue">属性值</param>
  470. /// <param name="nodes">选择节点</param>
  471. public void SetAttribute(string AttributeName, string AttributeValue, params string[] nodes)
  472. {
  473. //初始化xml
  474. XmlDocument xmlDoc = new XmlDocument();
  475. if (File.Exists(_xmlPath))
  476. xmlDoc.Load(_xmlPath);
  477. else
  478. xmlDoc.LoadXml("<XmlConfig />");
  479. XmlNode xmlRoot = xmlDoc.ChildNodes[0];
  480. //新增、编辑 节点
  481. string xpath = string.Join("/", nodes);
  482. XmlElement element = (XmlElement)xmlDoc.SelectSingleNode(xpath);
  483. if (element == null) //新增节点
  484. {
  485. element = (XmlElement)makeXPath(xmlDoc, xmlRoot, xpath);
  486. }
  487. //设置节点上属性的值
  488. element.SetAttribute(AttributeName, AttributeValue);
  489. //保存
  490. xmlDoc.Save(_xmlPath);
  491. }
  492. public string GetAttribute(string AttributeName, params string[] nodes)
  493. {
  494. //初始化xml
  495. XmlDocument xmlDoc = new XmlDocument();
  496. if (File.Exists(_xmlPath))
  497. xmlDoc.Load(_xmlPath);
  498. else
  499. xmlDoc.LoadXml("<XmlConfig />");
  500. XmlNode xmlRoot = xmlDoc.ChildNodes[0];
  501. //新增、编辑 节点
  502. string xpath = string.Join("/", nodes);
  503. XmlElement element = (XmlElement)xmlDoc.SelectSingleNode(xpath);
  504. if (element == null) //新增节点
  505. {
  506. element = (XmlElement)makeXPath(xmlDoc, xmlRoot, xpath);
  507. }
  508. //设置节点上属性的值
  509. string retstr = element.GetAttribute(AttributeName);
  510. //保存
  511. xmlDoc.Save(_xmlPath);
  512. return retstr;
  513. }
  514. /// <summary>
  515. /// 读取配置
  516. /// </summary>
  517. /// <param name="nodes">节点</param>
  518. /// <returns></returns>
  519. public string Read(params string[] nodes)
  520. {
  521. XmlDocument xmlDoc = new XmlDocument();
  522. if (File.Exists(_xmlPath) == false)
  523. return null;
  524. else
  525. xmlDoc.Load(_xmlPath);
  526. string xpath = string.Join("/", nodes);
  527. XmlNode node = xmlDoc.SelectSingleNode("/XmlConfig/" + xpath);
  528. if (node == null)
  529. return null;
  530. return node.InnerText;
  531. }
  532. /// <summary>
  533. /// 删除节点
  534. /// </summary>
  535. /// <param name="nodes"></param>
  536. public void RemoveNode(params string[] nodes)
  537. {
  538. XmlDocument xmlDoc = new XmlDocument();
  539. if (File.Exists(_xmlPath) == false)
  540. return;
  541. else
  542. xmlDoc.Load(_xmlPath);
  543. string xpath = string.Join("/", nodes);
  544. XmlNode node = xmlDoc.SelectSingleNode("/XmlConfig/" + xpath);
  545. //取得父节点
  546. string[] father_nodes = new string[nodes.Count() - 1];
  547. //对父节点进行初始化
  548. for (int i = 0; i < nodes.Count() - 1; i++)
  549. {
  550. father_nodes[i] = (string)nodes[i].Clone();
  551. }
  552. string fast_xpath = string.Join("/", father_nodes);
  553. XmlNode fastnode = xmlDoc.SelectSingleNode("/XmlConfig/" + fast_xpath);
  554. if (node == null)
  555. return;
  556. if (fastnode == null)
  557. return;
  558. //使用父节点删除子节点
  559. fastnode.RemoveChild(node);
  560. //保存
  561. xmlDoc.Save(_xmlPath);
  562. }
  563. //递归根据 xpath 的方式进行创建节点
  564. static private XmlNode makeXPath(XmlDocument doc, XmlNode parent, string xpath)
  565. {
  566. // 在XPath抓住下一个节点的名称;父级如果是空的则返回
  567. string[] partsOfXPath = xpath.Trim('/').Split('/');
  568. string nextNodeInXPath = partsOfXPath.First();
  569. if (string.IsNullOrEmpty(nextNodeInXPath))
  570. return parent;
  571. // 获取或从名称创建节点
  572. XmlNode node = parent.SelectSingleNode(nextNodeInXPath);
  573. if (node == null)
  574. node = parent.AppendChild(doc.CreateElement(nextNodeInXPath));
  575. // 加入的阵列作为一个XPath表达式和递归余数
  576. string rest = String.Join("/", partsOfXPath.Skip(1).ToArray());
  577. return makeXPath(doc, node, rest);
  578. }
  579. #endregion
  580. #region 特殊颗粒XML处理
  581. /// <summary>
  582. /// 特殊颗粒XML读取
  583. /// </summary>
  584. /// <param name="FilePath">文件地址</param>
  585. /// <param name="RegName">节点名称</param>
  586. /// <returns>DataSet</returns>
  587. public static DataSet GetXMLRegList(string FilePath, string TagName)
  588. {
  589. XmlDocument xmlDoc = new XmlDocument();
  590. xmlDoc.Load(FilePath); //加载Xml文件
  591. XmlNodeList nodeList = xmlDoc.GetElementsByTagName(TagName);
  592. List<DataSet> dsList = new List<DataSet>();
  593. DataSet dsResult = new DataSet();
  594. foreach (XmlNode node in nodeList)
  595. {
  596. XmlElement ele = (XmlElement)node;
  597. DataSet dsn = new DataSet();
  598. StringReader read = new StringReader(node.OuterXml);
  599. dsn.ReadXml(read);
  600. dsList.Add(dsn);
  601. }
  602. for (int i = 0; i < dsList.Count; i++)
  603. {
  604. if (i == 0)
  605. {
  606. dsResult = dsList[i];
  607. }
  608. else
  609. {
  610. dsResult.Merge(dsList[i], false, MissingSchemaAction.Ignore);
  611. }
  612. }
  613. return dsResult;
  614. }
  615. public static bool UpdateSpecialGrayXMLFile(string FilePath, DataGridView dg,bool isToRun)
  616. {
  617. XmlDocument xmlDoc = new XmlDocument();
  618. xmlDoc.Load(FilePath); //加载Xml文件
  619. xmlDoc.RemoveAll();
  620. XmlElement xmlData = xmlDoc.CreateElement("XMLData");
  621. xmlDoc.AppendChild(xmlData);
  622. if (isToRun)
  623. {
  624. xmlData.SetAttribute("ToRun", "True");
  625. }
  626. else
  627. {
  628. xmlData.SetAttribute("ToRun", "False");
  629. }
  630. XmlElement membercol = xmlDoc.CreateElement("Collection");
  631. membercol.SetAttribute("RegName", "GrayRangeList");
  632. xmlData.AppendChild(membercol);
  633. for (int i = 0; i < dg.Rows.Count; i++)
  634. {
  635. XmlElement member = xmlDoc.CreateElement("Member");
  636. membercol.AppendChild(member);
  637. for (int j = 0; j < dg.Columns.Count; j++)
  638. {
  639. //设置参数
  640. member.SetAttribute("RegName", dg.Rows[i].Cells["RegName"].Value.ToString());
  641. member.SetAttribute("start", dg.Rows[i].Cells["start"].Value.ToString());
  642. member.SetAttribute("end", dg.Rows[i].Cells["end"].Value.ToString());
  643. member.SetAttribute("diameterStart", dg.Rows[i].Cells["diameterStart"].Value.ToString());
  644. member.SetAttribute("diameterEnd", dg.Rows[i].Cells["diameterEnd"].Value.ToString());
  645. if (dg.Rows[i].Cells["collectXray"].Value==DBNull.Value || dg.Rows[i].Cells["collectXray"].Value.ToString() == "false")
  646. {
  647. member.SetAttribute("collectXray", "false");
  648. }
  649. else
  650. {
  651. member.SetAttribute("collectXray", "true");
  652. }
  653. }
  654. }
  655. xmlDoc.Save(FilePath);
  656. return true;
  657. }
  658. #endregion
  659. }
  660. }