XmlHlelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Xml;
  8. using IwbZero.ToolCommon.LogHelpers;
  9. namespace IwbZero.ToolCommon.FileHelpers
  10. {
  11. public static class XmlHelper
  12. {
  13. /// <summary>
  14. /// XML转换成DataTable
  15. /// </summary>
  16. /// <param name="xmlDS"></param>
  17. /// <returns></returns>
  18. public static string ConvertDataTableToXML(this DataTable xmlDS)
  19. {
  20. XmlTextWriter writer = null;
  21. try
  22. {
  23. var stream = new MemoryStream();
  24. writer = new XmlTextWriter(stream, Encoding.UTF8);
  25. xmlDS.WriteXml(writer);
  26. int count = (int)stream.Length;
  27. byte[] arr = new byte[count];
  28. stream.Seek(0, SeekOrigin.Begin);
  29. stream.Read(arr, 0, count);
  30. UTF8Encoding utf = new UTF8Encoding();
  31. return utf.GetString(arr).Trim();
  32. }
  33. catch (Exception e)
  34. {
  35. typeof(XmlHelper).LogError(e);
  36. return "";
  37. }
  38. finally
  39. {
  40. writer?.Close();
  41. }
  42. }
  43. /// <summary>
  44. /// XML转换成DataTable
  45. /// </summary>
  46. /// <param name="xmlData"></param>
  47. /// <returns></returns>
  48. public static DataSet ConvertXMLToDataSet(this string xmlData)
  49. {
  50. XmlTextReader reader = null;
  51. try
  52. {
  53. DataSet xmlDS = new DataSet();
  54. var stream = new StringReader(xmlData);
  55. reader = new XmlTextReader(stream);
  56. xmlDS.ReadXml(reader);
  57. return xmlDS;
  58. }
  59. catch (Exception e)
  60. {
  61. typeof(XmlHelper).LogError(e);
  62. return null;
  63. }
  64. finally
  65. {
  66. reader?.Close();
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// Object mapping of a xml file
  72. /// </summary>
  73. public class XModel
  74. {
  75. /// <summary>
  76. /// Directory of this xml file
  77. /// </summary>
  78. public string XmlDirectory { get; set; }
  79. /// <summary>
  80. /// Xml file name
  81. /// </summary>
  82. public string FileName { get; set; }
  83. /// <summary>
  84. /// Xml file encode
  85. /// </summary>
  86. public string Encode { get; set; }
  87. /// <summary>
  88. /// Root tag
  89. /// </summary>
  90. public XmlTag Root { get; set; }
  91. /// <summary>
  92. /// XmlDocument object of this xml file
  93. /// </summary>
  94. private XmlDocument XmlDoc { get; set; }
  95. #region constructor
  96. /// <summary>
  97. /// Default constructor
  98. /// </summary>
  99. public XModel()
  100. {
  101. }
  102. /// <summary>
  103. /// Constructor: Create XmlModel from XmlTag and save xml file
  104. /// </summary>
  105. /// <param name="directory">directory</param>
  106. /// <param name="fileName">fileName</param>
  107. /// <param name="encode">encode type</param>
  108. /// <param name="root">root tag</param>
  109. public XModel(string directory, string fileName, string encode, XmlTag root)
  110. {
  111. CreateWithFile(directory, fileName, encode, root);
  112. }
  113. /// <summary>
  114. /// Constructor: Create XmlModel from XmlTag only
  115. /// </summary>
  116. /// <param name="root">root tag</param>
  117. public XModel(XmlTag root)
  118. {
  119. CreateWithOutFile(root);
  120. }
  121. /// <summary>
  122. /// Constructor: Read XmlModel From xml file
  123. /// </summary>
  124. /// <param name="directory">directory</param>
  125. /// <param name="fileName">fileName</param>
  126. public XModel(string directory, string fileName)
  127. {
  128. Read(directory, fileName);
  129. }
  130. /// <summary>
  131. /// Constructor: Read XmlModel From xml string
  132. /// </summary>
  133. /// <param name="xml">xml string</param>
  134. public XModel(string xml)
  135. {
  136. Read(xml);
  137. }
  138. #endregion constructor
  139. #region Create and Read XmlModel
  140. /// <summary>
  141. /// Create XmlModel from XmlTag and save xml file
  142. /// </summary>
  143. /// <param name="directory">directory</param>
  144. /// <param name="fileName">fileName</param>
  145. /// <param name="encode">encode type</param>
  146. /// <param name="root">root tag</param>
  147. public void CreateWithFile(string directory, string fileName, string encode, XmlTag root)
  148. {
  149. XmlDirectory = directory;
  150. FileName = fileName;
  151. Encode = encode;
  152. Root = root;
  153. Save();
  154. }
  155. /// <summary>
  156. /// Create XmlModel from XmlTag only
  157. /// </summary>
  158. /// <param name="root"></param>
  159. public void CreateWithOutFile(XmlTag root)
  160. {
  161. Root = root;
  162. }
  163. /// <summary>
  164. /// Read XmlModel From xml file
  165. /// </summary>
  166. /// <param name="directory">directory</param>
  167. /// <param name="fileName">fileName</param>
  168. public void Read(string directory, string fileName)
  169. {
  170. XmlDirectory = directory;
  171. FileName = fileName;
  172. XmlDoc = new XmlDocument();
  173. XmlDoc.Load(directory + fileName);
  174. Root = NodeToTag(XmlDoc.DocumentElement);
  175. }
  176. /// <summary>
  177. /// Read XmlModel From xml string
  178. /// </summary>
  179. /// <param name="xml">xml string</param>
  180. public void Read(string xml)
  181. {
  182. XmlDoc = new XmlDocument();
  183. XmlDoc.LoadXml(xml);
  184. Root = NodeToTag(XmlDoc.DocumentElement);
  185. }
  186. #endregion Create and Read XmlModel
  187. /// <summary>
  188. /// Get current XmlModel's XmlDocument type object
  189. /// </summary>
  190. /// <returns></returns>
  191. public XmlDocument GetXmlDocument()
  192. {
  193. //create XmlModel xmldoc is null
  194. if (XmlDoc == null)
  195. {
  196. XmlDoc = new XmlDocument();
  197. }
  198. //replace root
  199. if (XmlDoc.DocumentElement != null)
  200. {
  201. XmlDoc.RemoveChild(XmlDoc.DocumentElement);
  202. }
  203. XmlDoc.AppendChild(TagToNode(Root));
  204. //add or edit declaration
  205. if (XmlDoc.FirstChild is XmlDeclaration declaration)
  206. {
  207. if (!string.IsNullOrEmpty(Encode))
  208. {
  209. declaration.Encoding = Encode;
  210. }
  211. }
  212. else
  213. {
  214. if (string.IsNullOrEmpty(Encode))
  215. {
  216. throw new Exception("Encode can't be empty");
  217. }
  218. XmlDoc.InsertBefore(XmlDoc.CreateXmlDeclaration("1.0", Encode, ""), XmlDoc.FirstChild);
  219. }
  220. return XmlDoc;
  221. }
  222. /// <summary>
  223. /// Save Object to file
  224. /// XmlDirectory and FileName and Encode need to be set before save
  225. /// </summary>
  226. public void Save()
  227. {
  228. if (Root == null)
  229. {
  230. throw new Exception("Root can't be null");
  231. }
  232. if (string.IsNullOrEmpty(XmlDirectory) || string.IsNullOrEmpty(FileName))
  233. {
  234. throw new Exception("XmlDirectory and FileName need to be set before save");
  235. }
  236. //generate xmldocument
  237. GetXmlDocument();
  238. if (!Directory.Exists(XmlDirectory))
  239. {
  240. Directory.CreateDirectory(XmlDirectory);
  241. }
  242. XmlDoc.Save(XmlDirectory + FileName);
  243. }
  244. /// <summary>
  245. /// Delete file
  246. /// </summary>
  247. public void Delete()
  248. {
  249. if (File.Exists(XmlDirectory + FileName))
  250. {
  251. File.Delete(XmlDirectory + FileName);
  252. }
  253. }
  254. /// <summary>
  255. /// Get xml string
  256. /// </summary>
  257. /// <returns></returns>
  258. public override string ToString()
  259. {
  260. return GetXmlDocument().OuterXml;
  261. }
  262. #region Helper
  263. /// <summary>
  264. /// Recurtion transfer XmlNode into XmlTag
  265. /// </summary>
  266. /// <param name="node">XmlNode</param>
  267. /// <returns></returns>
  268. public XmlTag NodeToTag(XmlNode node)
  269. {
  270. //不转化注释
  271. for (int i = 0; i < node.ChildNodes.Count; i++)
  272. {
  273. if (node.ChildNodes[i].Name == ("#comment"))
  274. {
  275. node.RemoveChild(node.ChildNodes[i]);
  276. }
  277. }
  278. //判断当前节点类型返回对应类型节点
  279. if (node.ChildNodes.Count > 0 && node.FirstChild.NodeType != XmlNodeType.Text)
  280. {
  281. XmlChildTag childTag = new XmlChildTag(node.Name);
  282. //属性不为空添加属性
  283. if (node.Attributes != null)
  284. {
  285. foreach (XmlAttribute attr in node.Attributes)
  286. {
  287. childTag.Attrs.Add(attr.Name, attr.Value);
  288. }
  289. }
  290. //递归添加子节点
  291. foreach (XmlNode childNode in node.ChildNodes)
  292. {
  293. XmlTag tag = NodeToTag(childNode);
  294. if (tag is XmlChildTag xmlChildTag)//根据子节点类型加入对应列表
  295. {
  296. childTag.ChildTagList.Add(xmlChildTag);
  297. }
  298. else
  299. {
  300. childTag.BaseTagList.Add(tag as XmlBaseTag);
  301. }
  302. }
  303. return childTag;
  304. }
  305. else
  306. {
  307. XmlBaseTag baseTag = new XmlBaseTag(node.Name);
  308. //属性不为空添加属性
  309. if (node.Attributes != null)
  310. {
  311. foreach (XmlAttribute attr in node.Attributes)
  312. {
  313. baseTag.Attrs.Add(attr.Name, attr.Value);
  314. }
  315. }
  316. baseTag.InnerText = node.InnerText;
  317. return baseTag;
  318. }
  319. }
  320. /// <summary>
  321. /// Recurtion transfer XmlTag into XmlNode
  322. /// </summary>
  323. /// <param name="tag">XmlTag</param>
  324. /// <returns></returns>
  325. public XmlNode TagToNode(XmlTag tag)
  326. {
  327. XmlNode node = XmlDoc.CreateElement(tag.Name);
  328. foreach (var attr in tag.Attrs)
  329. {
  330. XmlAttribute xmlAttr = XmlDoc.CreateAttribute(attr.Key);
  331. xmlAttr.Value = attr.Value;
  332. node.Attributes?.Append(xmlAttr);
  333. }
  334. if (tag is XmlChildTag childTag)
  335. {
  336. if (childTag.BaseTagList.Count == 0 && childTag.ChildTagList.Count == 0)
  337. {
  338. throw new Exception("A XmlChildTag " + childTag.Name + "'s BaseTagList and ChildTagList can't both empty,if this tag don't have any child please use XmlBaseTag type");
  339. }
  340. //添加对应的子节点
  341. foreach (XmlBaseTag innerTag in childTag.BaseTagList)
  342. {
  343. node.AppendChild(TagToNode(innerTag));
  344. }
  345. foreach (XmlChildTag innerTag in childTag.ChildTagList)
  346. {
  347. node.AppendChild(TagToNode(innerTag));
  348. }
  349. }
  350. else
  351. {
  352. if (tag is XmlBaseTag baseTag) node.InnerText = baseTag.InnerText;
  353. }
  354. return node;
  355. }
  356. /// <summary>
  357. /// Quickly get a XmlChildTag
  358. /// </summary>
  359. /// <param name="tagNames">tag's name input by order</param>
  360. /// <returns></returns>
  361. public XmlChildTag GetChildTag(params string[] tagNames)
  362. {
  363. if (tagNames[0] != Root.Name)
  364. {
  365. throw new Exception("Root tag doesn't match");
  366. }
  367. if (!(Root is XmlChildTag))
  368. {
  369. throw new Exception("Root " + tagNames[0] + " must be XmlChildTag type");
  370. }
  371. XmlChildTag result = (XmlChildTag)Root;
  372. for (int i = 1; i < tagNames.Length; i++)
  373. {
  374. if (result != null && result.ChildTagList.All(t => t.Name != tagNames[i]))
  375. {
  376. throw new Exception("Can't find XmlChildTag tag " + tagNames[i] + " by the input tag names with this order");
  377. }
  378. result = result?.ChildTagList.FirstOrDefault(t => t.Name == tagNames[i]);
  379. }
  380. return result;
  381. }
  382. /// <summary>
  383. /// Quickly get a XmlBaseTag
  384. /// </summary>
  385. /// <param name="tagNames">按顺序输入的节点名称/tag's name input by order</param>
  386. /// <returns></returns>
  387. public XmlBaseTag GetBaseTag(params string[] tagNames)
  388. {
  389. if (tagNames[0] != Root.Name)
  390. {
  391. throw new Exception("Root tag doesn't match");
  392. }
  393. if (Root is XmlBaseTag tag)
  394. {
  395. if (tagNames.Length == 1)
  396. {
  397. return tag;
  398. }
  399. throw new Exception("Root " + tagNames[0] + " is XmlBaseTag type, don't have any childs");
  400. }
  401. XmlChildTag childTag = Root as XmlChildTag;
  402. for (int i = 1; i < tagNames.Length; i++)
  403. {
  404. if (childTag != null && (childTag.ChildTagList.All(t => t.Name != tagNames[i]) && childTag.BaseTagList.All(t => t.Name != tagNames[i])))
  405. {
  406. throw new Exception("Can't find XmlTag " + tagNames[i] + " by the input tag names with this order");
  407. }
  408. //最后一个节点查找BaseTagList,否则查找ChildTagList
  409. if (i == tagNames.Length - 1)
  410. {
  411. if (childTag != null && childTag.BaseTagList.Any(t => t.Name == tagNames[i]))
  412. {
  413. return childTag.BaseTagList.FirstOrDefault(t => t.Name == tagNames[i]);
  414. }
  415. throw new Exception("Tag:" + tagNames[i] + " must be XmlBaseTag type");
  416. }
  417. //找到对应XmlChildTag节点缩小查找范围
  418. if (childTag != null && childTag.ChildTagList.Any(t => t.Name == tagNames[i]))
  419. {
  420. childTag = childTag.ChildTagList.FirstOrDefault(t => t.Name == tagNames[i]);
  421. }
  422. else
  423. {
  424. throw new Exception("Tag:" + tagNames[i] + " must be XmlChildTag type");
  425. }
  426. }
  427. return null;
  428. }
  429. #endregion Helper
  430. }
  431. /// <summary>
  432. /// XmlTag: A xml tag
  433. /// </summary>
  434. public abstract class XmlTag
  435. {
  436. /// <summary>
  437. /// Name
  438. /// </summary>
  439. public string Name { get; set; }
  440. /// <summary>
  441. /// Attributes
  442. /// </summary>
  443. public Dictionary<string, string> Attrs { get; set; }
  444. }
  445. /// <summary>
  446. /// XmlChildTag: A XmlTag that at least have a XmlChildTag or XmlBaseTag inside
  447. /// </summary>
  448. public class XmlChildTag : XmlTag
  449. {
  450. /// <summary>
  451. /// XmlChildTags of this XmlChildTag
  452. /// </summary>
  453. public List<XmlChildTag> ChildTagList { get; set; }
  454. /// <summary>
  455. /// XmlBaseTags of this XmlChildTag
  456. /// </summary>
  457. public List<XmlBaseTag> BaseTagList { get; set; }
  458. /// <summary>
  459. /// Constructor
  460. /// </summary>
  461. /// <param name="name">Tag's Name</param>
  462. public XmlChildTag(string name)
  463. {
  464. Name = name;
  465. Attrs = new Dictionary<string, string>();
  466. ChildTagList = new List<XmlChildTag>();
  467. BaseTagList = new List<XmlBaseTag>();
  468. }
  469. /// <summary>
  470. /// Get a XmlChildTag from this tag
  471. /// </summary>
  472. /// <param name="tagNames">tag's name input by order</param>
  473. /// <returns></returns>
  474. public XmlChildTag GetChildTag(params string[] tagNames)
  475. {
  476. XmlChildTag result = this;
  477. foreach (var tagName in tagNames)
  478. {
  479. if (result != null && result.ChildTagList.All(t => t.Name != tagName))
  480. {
  481. throw new Exception("Can't find XmlChildTag tag " + tagName + " by the input tag names with this order");
  482. }
  483. result = result?.ChildTagList.FirstOrDefault(t => t.Name == tagName);
  484. }
  485. return result;
  486. }
  487. /// <summary>
  488. /// Get a XmlBaseTag from this tag
  489. /// </summary>
  490. /// <param name="tagNames">tag's name input by order</param>
  491. /// <returns></returns>
  492. public XmlBaseTag GetBaseTag(params string[] tagNames)
  493. {
  494. XmlChildTag childTag = this;
  495. for (int i = 0; i < tagNames.Length; i++)
  496. {
  497. if (childTag != null && (childTag.ChildTagList.All(t => t.Name != tagNames[i]) && childTag.BaseTagList.All(t => t.Name != tagNames[i])))
  498. {
  499. throw new Exception("Can't find XmlTag tag " + tagNames[i] + " by the input tag names with this order");
  500. }
  501. //最后一个节点查找BaseTagList,否则查找ChildTagList
  502. if (i == tagNames.Length - 1)
  503. {
  504. if (childTag != null && childTag.BaseTagList.Any(t => t.Name == tagNames[i]))
  505. {
  506. return childTag.BaseTagList.FirstOrDefault(t => t.Name == tagNames[i]);
  507. }
  508. throw new Exception("Tag:" + tagNames[i] + " must be XmlBaseTag type");
  509. }
  510. //找到对应XmlChildTag节点缩小查找范围
  511. if (childTag != null && childTag.ChildTagList.Any(t => t.Name == tagNames[i]))
  512. {
  513. childTag = childTag.ChildTagList.FirstOrDefault(t => t.Name == tagNames[i]);
  514. }
  515. else
  516. {
  517. throw new Exception("Tag:" + tagNames[i] + " must be XmlChildTag type");
  518. }
  519. }
  520. return null;
  521. }
  522. }
  523. /// <summary>
  524. /// Xml基础标签:基础标签内不能有子标签,可以有内部文字
  525. /// XmlBaseTag:XmlBaseTag don't have child tag, but inner text
  526. /// </summary>
  527. public class XmlBaseTag : XmlTag
  528. {
  529. /// <summary>
  530. /// InnerText
  531. /// </summary>
  532. public string InnerText { get; set; }
  533. /// <summary>
  534. /// Constructor
  535. /// </summary>
  536. /// <param name="name">Tag's Name</param>
  537. public XmlBaseTag(string name)
  538. {
  539. Name = name;
  540. Attrs = new Dictionary<string, string>();
  541. }
  542. }
  543. #region Test
  544. ////Create a xmlfile
  545. //public static void Create()
  546. //{
  547. // XmlChildTag rootTag = new XmlChildTag("Root");
  548. // rootTag.BaseTagList.Add(new XmlBaseTag("BaseTagOne"));
  549. // XModel myXModel = new XModel("D:\\XML\\", "MyXModel.xml", "utf-8", rootTag);
  550. // Console.WriteLine("Create success");
  551. //}
  552. ////Read a xml file
  553. //public static void Read()
  554. //{
  555. // XModel myXml = new XModel("D:\\XML\\", "MyXModel.xml");
  556. // string rootName = myXml.Root.Name;
  557. // string baseTagOneName = (myXml.Root as XmlChildTag).BaseTagList.First().Name;
  558. // Console.WriteLine("Read success");
  559. //}
  560. ////Edit a xml file
  561. //public static void Edit()
  562. //{
  563. // XModel myXml = new XModel("D:\\XML\\", "MyXModel.xml");
  564. // //edit root name and attributes
  565. // XmlChildTag root = myXml.Root as XmlChildTag;
  566. // root.Name = "NewRoot";
  567. // root.Attrs.Add("attr1", "value1");
  568. // //root add a child tag which have three base tag
  569. // XmlChildTag newChild = new XmlChildTag("NewChild");
  570. // newChild.BaseTagList.Add(new XmlBaseTag("BaseTagTwo"));
  571. // XmlBaseTag baseTagThree = new XmlBaseTag("BaseTagThree");
  572. // baseTagThree.Attrs.Add("testa", "testv");
  573. // newChild.BaseTagList.Add(baseTagThree);
  574. // newChild.BaseTagList.Add(new XmlBaseTag("BaseTagFour") { InnerText = "some text" });
  575. // root.ChildTagList.Add(newChild);
  576. // //save to file
  577. // myXml.Save();
  578. // Console.WriteLine("Edit success");
  579. //}
  580. ////Quickly Get and edit
  581. //public static void QuicklyGet()
  582. //{
  583. // XModel myXml = new XModel("D:\\XML\\", "MyXModel.xml");
  584. // //GetChildTag
  585. // XmlChildTag childA = myXml.GetChildTag("NewRoot", "NewChild");
  586. // childA.Attrs.Add("find", "ture");
  587. // //GetBaseTag
  588. // XmlBaseTag baseTagTwo = myXml.GetBaseTag("NewRoot", "NewChild", "BaseTagTwo");
  589. // baseTagTwo.InnerText = "default return first tag be found";
  590. // //quickly get tag from a XmlChildTag
  591. // XmlChildTag childB = myXml.GetChildTag("NewRoot", "NewChild");
  592. // //lambda
  593. // XmlBaseTag target = childB.BaseTagList.Where(b => b.Attrs.Keys.Contains("testa")).First();
  594. // target.InnerText = "quickly get combine with lambda let you read write xml file very quick";
  595. // //continue quickly get
  596. // XmlBaseTag target2 = childB.GetBaseTag("BaseTagTwo");
  597. // target2.InnerText = "quickly get can be use on XmlChildTag too.";
  598. // myXml.Save();
  599. // Console.WriteLine("QuicklyGet success");
  600. //}
  601. ////Delete
  602. //public static void Delete()
  603. //{
  604. // XModel myXml = new XModel("D:\\XML\\", "MyXModel.xml");
  605. // myXml.Delete();
  606. // Console.WriteLine("Delete success");
  607. //}
  608. ////To string
  609. //public static void PrintXModel()
  610. //{
  611. // XModel myXml = new XModel("D:\\XML\\", "MyXModel.xml");
  612. // Console.WriteLine(myXml.ToString());
  613. //}
  614. ////Create from tag only
  615. //public static void CreateWithOutFile()
  616. //{
  617. // XmlChildTag root = new XmlChildTag("root");
  618. // root.BaseTagList.Add(new XmlBaseTag("base") { InnerText = "test" });
  619. // XModel myXml = new XModel(root);
  620. // myXml.XmlDirectory = "d:\\";
  621. // myXml.FileName = "nofile.xml";
  622. // myXml.Encode = "utf-8";
  623. // myXml.Save();
  624. //}
  625. ////Read from xml String
  626. //public static void ReadXmlString()
  627. //{
  628. // XModel newxml = new XModel(new XModel("D:\\XML\\", "MyXModel.xml").ToString());
  629. // newxml.Root.Name = "newroot";
  630. // newxml.XmlDirectory = "d:\\";
  631. // newxml.FileName = "newxml.xml";
  632. // newxml.Encode = "gbk";
  633. // newxml.Save();
  634. //}
  635. #endregion Test
  636. }