C#操作XML学习之创建XML文件的同时新建根节点和子节点(多级子节点)





https://www.cnblogs.com/zhyue93/archive/2015/10/01/xml1.html

最近工作中遇到一个问题,要求创建一个XML文件,在创建的时候要初始化该XML文档,同时该文档打开后是XML形式,但是后缀名不是。在网上找了好些资料没找到,只能自己试着弄了一下,没想到成功了,把它记下来作为自己的学习笔记。

需求:创建XML文件,后缀名为.xwsp

初始化的文档节点如下:

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <xxxversion="1.0" name="aaa">
 3     <CreationInfo>
 4         <CreatedBy>CreateUser</CreatedBy>
 5         <CreatedTime>2015/10/1 14:03:48</CreatedTime>
 6         <SavedTime>2015/10/1 14:03:48</SavedTime>
 7     </CreationInfo>
 8     <a/>
 9     <b/>
10     <c/>
11 </xxx>
复制代码
View Code

首先第一个问题:后缀名为.xwsp,打开后显示的XML文本

当时这个问题想复杂了,因为要进行二进制转换之类的,网上找了老半天没找到,最后自己试了一下,简单的要死,只能说自己笨

解决方法:xmlDoc.Save("a.xwsp");

只要保存xml文件的时候改了后缀名即可,我也是醉了

 

第二个问题:添加节点的时候尤其是添加<CreatedBy><CreatedTime><SavedTime>这三个节点的时候老是添加不进去

当时写的代码如下:

复制代码
 1 private static void CreateXwspFile(string fileName, string path)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             //创建类型声明节点  
 5             XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
 6             xmlDoc.AppendChild(xdDec);
 7 
 8             //创建根节点  
 9             XmlElement xeRoot = xmlDoc.CreateElement("xxx");
10             //给节点属性赋值
11             xeRoot.SetAttribute("version", "1.0");
12             xeRoot.SetAttribute("name", fileName);
13             xmlDoc.AppendChild(xeRoot);
14 
15             //创建并添加<CreationInfo></CreationInfo>节点
16             xeRoot = xmlDoc.CreateElement("CreationInfo");
17             XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");
18             if (xnXwsp != null)
19             {
20                 xnXwsp.AppendChild(xeRoot);
21             }
22 
23             //创建并添加<CreatedBy></CreatedBy>节点
24             xeRoot = xmlDoc.CreateElement("CreatedBy");
25             xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
26             if (xnXwsp != null)
27             {
28                 xnXwsp.AppendChild(xeRoot);
29             }
30 
31             //创建并添加<CreatedTime></CreatedTime>节点
32             xeRoot = xmlDoc.CreateElement("CreatedTime");
33             xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
34             if (xnXwsp != null)
35             {
36                 xnXwsp.AppendChild(xeRoot);
37             }
38 
39             //创建并添加<SavedTime></SavedTime>节点
40             xeRoot = xmlDoc.CreateElement("SavedTime");
41             xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
42             if (xnXwsp != null)
43             {
44                 xnXwsp.AppendChild(xeRoot);
45             }
46 
47             //创建并添加<a></a>节点
48             xeRoot = xmlDoc.CreateElement("a");
49             xnXwsp = xmlDoc.SelectSingleNode("xxx");
50             if (xnXwsp != null)
51             {
52                 xnXwsp.AppendChild(xeRoot);
53             }
54             //创建并添加<b></b>节点
55             xeRoot = xmlDoc.CreateElement("b");
56             xnXwsp = xmlDoc.SelectSingleNode("xxx");
57             if (xnXwsp != null)
58             {
59                 xnXwsp.AppendChild(xeRoot);
60             }
61             //创建并添加<c></c>节点
62             xeRoot = xmlDoc.CreateElement("c");
63             xnXwsp = xmlDoc.SelectSingleNode("xxx");
64             if (xnXwsp != null)
65             {
66                 xnXwsp.AppendChild(xeRoot);
67             }
68             //保存XML文档
69             try
70             {
71                 xmlDoc.Save(path + fileName + ".xwsp");
72             }
73             catch (Exception ep)
74             {
75                 Console.WriteLine(ep.Message);
76             }
77         }
复制代码
View Code

结果如下:

复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <xxx version="1.0" name="workspace1">
3   <CreationInfo />
4   <a />
5   <b />
6   <c />
7 </xxx>
复制代码
View Code

子节点<CreatedBy><CreatedTime><SavedTime>死活出不来,打断点<CreationInfo>节点先添加进去了,但是xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");这一句的结果死活为null,想不通,现在都没想通,后来换了一种写法就OK了

这种得不到正确写法的思路是:先添加父节点<CreationInfo>再添加子节点<CreatedBy><CreatedTime><SavedTime>

网上找到另一种写法,思路是:先创建子节点<CreatedBy><CreatedTime><SavedTime>,再创建父节点<CreationInfo>,然后把子节点添加到该父节点下面,再查找根节点<xxx>,最后把父节点<CreationInfo>添加到根节点末尾就OK了,代码如下:

复制代码
 1 private static void CreateXwspFile(string fileName, string path)
 2         {
 3             XmlDocument xmlDoc = new XmlDocument();
 4             //创建类型声明节点  
 5             XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
 6             xmlDoc.AppendChild(xdDec);
 7 
 8             //创建根节点  
 9             XmlElement xeRoot = xmlDoc.CreateElement("xxx");
10             //给节点属性赋值
11             xeRoot.SetAttribute("version", "1.0");
12             xeRoot.SetAttribute("name", fileName);
13             xmlDoc.AppendChild(xeRoot);
14 
15             ////创建并添加<CreationInfo></CreationInfo>节点
16             ////创建并添加<CreatedBy></CreatedBy>节点
17             ////创建并添加<CreatedTime></CreatedTime>节点
18             ////创建并添加<SavedTime></SavedTime>节点
19             XmlElement xeCreationInfo = xmlDoc.CreateElement("CreationInfo");
20             XmlElement xeCreatedBy = xmlDoc.CreateElement("CreatedBy");
21             xeCreatedBy.InnerText = "Tektronix Course Editor";
22             XmlElement xeCreatedTime = xmlDoc.CreateElement("CreatedTime");
23             xeCreatedTime.InnerText = DateTime.Now.ToString();
24             XmlElement xeSavedTime = xmlDoc.CreateElement("SavedTime");
25             xeSavedTime.InnerText = DateTime.Now.ToString();
26             xeCreationInfo.AppendChild(xeCreatedBy);
27             xeCreationInfo.AppendChild(xeCreatedTime);
28             xeCreationInfo.AppendChild(xeSavedTime);
29             XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");
30             if (xnXwsp != null)
31             {
32                 xnXwsp.AppendChild(xeCreationInfo);
33             }
34 
35             //创建并添加<a></a>节点
36             xeRoot = xmlDoc.CreateElement("a");
37             if (xnXwsp != null)
38             {
39                 xnXwsp.AppendChild(xeRoot);
40             }
41             //创建并添加<b></b>节点
42             xeRoot = xmlDoc.CreateElement("b");
43             if (xnXwsp != null)
44             {
45                 xnXwsp.AppendChild(xeRoot);
46             }
47             //创建并添加<c></c>节点
48             xeRoot = xmlDoc.CreateElement("c");
49             if (xnXwsp != null)
50             {
51                 xnXwsp.AppendChild(xeRoot);
52             }
53             //保存XML文档
54             try
55             {
56                 xmlDoc.Save(path + fileName + ".xwsp");
57             }
58             catch (Exception ep)
59             {
60                 Console.WriteLine(ep.Message);
61             }
62         }
复制代码
View Code

结果如下:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <xxx version="1.0" name="workspace1">
 3   <CreationInfo>
 4     <CreatedBy>Tektronix Course Editor</CreatedBy>
 5     <CreatedTime>2015/10/1 14:43:56</CreatedTime>
 6     <SavedTime>2015/10/1 14:43:57</SavedTime>
 7   </CreationInfo>
 8   <a />
 9   <b />
10   <c />
11 </xxx>
复制代码
View Code

 

现在还有一个问题没有解决:

要在<?xml version="1.0" encoding="utf-8"?>节点的后面插入<!DOCTYPE xwsp>这个节点,不晓得该怎么做,而且后面那个xwsp是可以改变的,意思就是可以自己定义,比如说我可以把它改为aaa之类的,这个暂时还没找到解决方法

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值