C#操作XML学习之创建XML文件的同时新建根节点和子节点(多级子节点)
最近工作中遇到一个问题,要求创建一个XML文件,在创建的时候要初始化该XML文档,同时该文档打开后是XML形式,但是后缀名不是。在网上找了好些资料没找到,只能自己试着弄了一下,没想到成功了,把它记下来作为自己的学习笔记。
需求:创建XML文件,后缀名为.xwsp
初始化的文档节点如下:
<?xml version="1.0" encoding="UTF-8"?>
<xxxversion="1.0" name="aaa">
<CreationInfo>
<CreatedBy>CreateUser</CreatedBy>
<CreatedTime>2015/10/1 14:03:48</CreatedTime>
<SavedTime>2015/10/1 14:03:48</SavedTime>
</CreationInfo>
<a/>
<b/>
<c/>
</xxx>
首先第一个问题:后缀名为.xwsp,打开后显示的XML文本
当时这个问题想复杂了,因为要进行二进制转换之类的,网上找了老半天没找到,最后自己试了一下,简单的要死,只能说自己笨
解决方法:xmlDoc.Save("a.xwsp");
只要保存xml文件的时候改了后缀名即可,我也是醉了
第二个问题:添加节点的时候尤其是添加<CreatedBy><CreatedTime><SavedTime>这三个节点的时候老是添加不进去
当时写的代码如下:
private static void CreateXwspFile(string fileName, string path)
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xdDec); //创建根节点
XmlElement xeRoot = xmlDoc.CreateElement("xxx");
//给节点属性赋值
xeRoot.SetAttribute("version", "1.0");
xeRoot.SetAttribute("name", fileName);
xmlDoc.AppendChild(xeRoot); //创建并添加<CreationInfo></CreationInfo>节点
xeRoot = xmlDoc.CreateElement("CreationInfo");
XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
} //创建并添加<CreatedBy></CreatedBy>节点
xeRoot = xmlDoc.CreateElement("CreatedBy");
xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
} //创建并添加<CreatedTime></CreatedTime>节点
xeRoot = xmlDoc.CreateElement("CreatedTime");
xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
} //创建并添加<SavedTime></SavedTime>节点
xeRoot = xmlDoc.CreateElement("SavedTime");
xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
} //创建并添加<a></a>节点
xeRoot = xmlDoc.CreateElement("a");
xnXwsp = xmlDoc.SelectSingleNode("xxx");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//创建并添加<b></b>节点
xeRoot = xmlDoc.CreateElement("b");
xnXwsp = xmlDoc.SelectSingleNode("xxx");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//创建并添加<c></c>节点
xeRoot = xmlDoc.CreateElement("c");
xnXwsp = xmlDoc.SelectSingleNode("xxx");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//保存XML文档
try
{
xmlDoc.Save(path + fileName + ".xwsp");
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
}
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<xxx version="1.0" name="workspace1">
<CreationInfo />
<a />
<b />
<c />
</xxx>
子节点<CreatedBy><CreatedTime><SavedTime>死活出不来,打断点<CreationInfo>节点先添加进去了,但是xnXwsp = xmlDoc.SelectSingleNode("CreationInfo");这一句的结果死活为null,想不通,现在都没想通,后来换了一种写法就OK了
这种得不到正确写法的思路是:先添加父节点<CreationInfo>再添加子节点<CreatedBy><CreatedTime><SavedTime>
网上找到另一种写法,思路是:先创建子节点<CreatedBy><CreatedTime><SavedTime>,再创建父节点<CreationInfo>,然后把子节点添加到该父节点下面,再查找根节点<xxx>,最后把父节点<CreationInfo>添加到根节点末尾就OK了,代码如下:
private static void CreateXwspFile(string fileName, string path)
{
XmlDocument xmlDoc = new XmlDocument();
//创建类型声明节点
XmlDeclaration xdDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDoc.AppendChild(xdDec); //创建根节点
XmlElement xeRoot = xmlDoc.CreateElement("xxx");
//给节点属性赋值
xeRoot.SetAttribute("version", "1.0");
xeRoot.SetAttribute("name", fileName);
xmlDoc.AppendChild(xeRoot); ////创建并添加<CreationInfo></CreationInfo>节点
////创建并添加<CreatedBy></CreatedBy>节点
////创建并添加<CreatedTime></CreatedTime>节点
////创建并添加<SavedTime></SavedTime>节点
XmlElement xeCreationInfo = xmlDoc.CreateElement("CreationInfo");
XmlElement xeCreatedBy = xmlDoc.CreateElement("CreatedBy");
xeCreatedBy.InnerText = "Tektronix Course Editor";
XmlElement xeCreatedTime = xmlDoc.CreateElement("CreatedTime");
xeCreatedTime.InnerText = DateTime.Now.ToString();
XmlElement xeSavedTime = xmlDoc.CreateElement("SavedTime");
xeSavedTime.InnerText = DateTime.Now.ToString();
xeCreationInfo.AppendChild(xeCreatedBy);
xeCreationInfo.AppendChild(xeCreatedTime);
xeCreationInfo.AppendChild(xeSavedTime);
XmlNode xnXwsp = xmlDoc.SelectSingleNode("xxx");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeCreationInfo);
} //创建并添加<a></a>节点
xeRoot = xmlDoc.CreateElement("a");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//创建并添加<b></b>节点
xeRoot = xmlDoc.CreateElement("b");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//创建并添加<c></c>节点
xeRoot = xmlDoc.CreateElement("c");
if (xnXwsp != null)
{
xnXwsp.AppendChild(xeRoot);
}
//保存XML文档
try
{
xmlDoc.Save(path + fileName + ".xwsp");
}
catch (Exception ep)
{
Console.WriteLine(ep.Message);
}
}
结果如下:
<?xml version="1.0" encoding="utf-8"?>
<xxx version="1.0" name="workspace1">
<CreationInfo>
<CreatedBy>Tektronix Course Editor</CreatedBy>
<CreatedTime>2015/10/1 14:43:56</CreatedTime>
<SavedTime>2015/10/1 14:43:57</SavedTime>
</CreationInfo>
<a />
<b />
<c />
</xxx>
现在还有一个问题没有解决:
要在<?xml version="1.0" encoding="utf-8"?>节点的后面插入<!DOCTYPE xwsp>这个节点,不晓得该怎么做,而且后面那个xwsp是可以改变的,意思就是可以自己定义,比如说我可以把它改为aaa之类的,这个暂时还没找到解决方法
C#操作XML学习之创建XML文件的同时新建根节点和子节点(多级子节点)的更多相关文章
- solr 6.0 没有schema.xml未自动创建schema文件
solr 6.0 没有schema.xml未自动创建schema文件 摘要:在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中 ...
- XML学习总结(二)——XML入门
XML学习总结(二)——XML入门 一.XML语法学习 学习XML语法的目的就是编写XML 一个XML文件分为如下几部分内容: 文档声明 元素 属性 注释 CDATA区 .特殊字符 处理指令(proc ...
- XML学习笔记(1)--XML概述
XML基本概念 XML—extensible Markup Language(可扩展标记语言) XML最基本的三个概念 1)XML语言---描述事物本身(可扩展) 2)XSL语言---展现事物表现形式 ...
- 2.6.1 XML配置:创建XML文件
(1) 工程名右击---New--file -- newfile窗口中:filename中输入testng.xml testng.xml 文件中打开后,切换到source 标签中.进行编辑. 内容 ...
- XML学习——java解析xml文件
递归获取每个标签 package test; import java.io.File; import java.util.List; import org.dom4j.Document; import ...
- XML学习总结(一)——XML介绍
一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...
- XML学习笔记——关于XML解析器
本篇文章基于W3C而写 在Firefox及其他浏览器中的XML解析器(除IE) var xmlDoc=document.implementation.createDocument("&quo ...
- XML学习笔记之XML的简介
最近,自学了一段时间xml,希望通过学习笔记的整理能够巩固一下知识点,也希望把知识分享给你们(描红字段为重点): XML(extensible Markup language):可扩展的标记语言,解决 ...
- C# -- 使用XmlDocument或XDocument创建xml文件
使用XmlDocument或XDocument创建xml文件 需引用:System.Xml; System.Xml.Linq; 1.使用XmlDocument创建xml(入门案例) static vo ...
随机推荐
- 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求
系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目 实战使 ...
- save与persist差别
唯一差别: 在没提交事务情况下 save会产生insert语句,然后因为没提交事务进行回滚. 而这种情况,persist是连insert语句都不会产生.
- 写js写傻了,明天研究一下异步
在html某元素上绑定一个click事件,该事件是一个执行事件很长的函数,比如执行几十亿或几百亿次加法,那么在这个函数执行的过程中,其他元素绑定的事件,是如何触发的呢,异步触发还是同步,触发时是怎么执 ...
- 大叔也说Xamarin~Android篇~Activity之间传递数组
回到目录 我们在开发应用程序时,不可能只使用一个Layout或者一个Activity,比如你个管理系统,要求用户先登陆然后再使用,这时你至少要有两个activity吧,先登陆一个,然后成功后需要跳到别 ...
- Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate).
Atitit 循环(loop), 递归(recursion), 遍历(traversal), 迭代(iterate). 1.1. 循环算是最基础的概念, 凡是重复执行一段代码, 都可以称之为循环. ...
- 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序
学习ASP.NET MVC系列: 学习ASP.NET MVC(一)——我的第一个ASP.NET MVC应用程序 学习ASP.NET MVC(二)——我的第一个ASP.NET MVC 控制器 学习ASP ...
- 手机页面的 HTML<meta> 标签使用与说明
name="viewport" 设置窗口(网页可绘制的区域) width="device-width" 应用宽与屏幕的宽一样的 (height同width) i ...
- gulp学习笔记4
gulp系列学习笔记: 1.gulp学习笔记1 2.gulp学习笔记2 3.gulp学习笔记3 4.gulp学习笔记4 之前的任务都是单个的,比较简单.接下去我们开始引用多个插件,一次性把任务搞定,省 ...
- c#将list集合转换为datatable的简单办法
public static class ExtensionMethods { /// <summary> /// 将List转换成DataTabl ...
- Mina、Netty、Twisted一起学(五):整合protobuf
protobuf是谷歌的Protocol Buffers的简称,用于结构化数据和字节码之间互相转换(序列化.反序列化),一般应用于网络传输,可支持多种编程语言. protobuf如何使用这里不再介绍, ...