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 ...
随机推荐
- REST建模语言RAML介绍
原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com RAML是什么? RAML是一种简洁的RESTful API描述性语言,它基于 YAML和JSON这样的公 ...
- H5 调用摄像头
WebRTC(Web Real-Time Communication,网页实时通信),是一个支持网页浏览器进行实时语音对话或视频对话的API. 1.getUserMedia 要播放摄像头的影像,首先需 ...
- asp.net 站点重启
有时一些特殊情况需要重启站点,在System.Web.dll程序集下HttpRuntime类下有一个静态方法UnloadAppDomain,使用这个方法可以重启站点: protected void b ...
- 将图片的二进制字节字符串在HTML页面以图片形式输出
具体实现代码如下: 1.新建一个一般处理程序: Image.ashx using System; using System.Collections.Generic; using System.Linq ...
- cordova添加plugin
cordova添加plugin #在线安装 cordova create chankoujie com.example.chankoujie ChanKouJie cordova plugin add ...
- 关于OpenVPN的入门使用
关于OpenVPN的入门使用 1.1源代码编译安装的初步了解 1.2 安装OpenVPN 1.3 生成证书.服务器端证书.客户端证书 1.4 关于server.ovpm & client.ov ...
- SSIS的CheckPoint用法
在SSIS的Package Property中有CheckPoints的属性目录,CheckPoint是SSIS的Failover Feature.通过简单的配置CheckPoint,能够在Packa ...
- 让BI告诉你:圣诞老人去哪了?
刚看到一篇关于圣诞节BI分析的文章,觉得很有意思,特来翻译了下和大家一起分享(可惜的是文章发布的时间有点久). 伴随着圣诞节即将到来的日子,POWER BI团队来回答大家最为关注的一个问题:圣诞老人到 ...
- php的mysql\mysqli\PDO(一)mysql
原文链接:http://www.orlion.ga/1140/ 工作中数据库的操作都被封装好了,这些怎么用的都快忘了干脆写篇博客重新复习下,以后要是再忘记了可以看这篇文章. PHP 5.5.0 起已废 ...
- struts2获取web元素(request、session、application)
一.Action中获取 第一种方式: 通过ActionContext,这种方式取到的对象是Map类型 import java.util.Map; import com.opensymphony.xwo ...