最近工作中遇到一个问题,要求创建一个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文件的同时新建根节点和子节点(多级子节点)的更多相关文章

  1. solr 6.0 没有schema.xml未自动创建schema文件

    solr 6.0 没有schema.xml未自动创建schema文件 摘要:在之前的Solr版本中(Solr5之前),在创建core的时候,Solr会自动创建好schema.xml,但是在之后的版本中 ...

  2. XML学习总结(二)——XML入门

    XML学习总结(二)——XML入门 一.XML语法学习 学习XML语法的目的就是编写XML 一个XML文件分为如下几部分内容: 文档声明 元素 属性 注释 CDATA区 .特殊字符 处理指令(proc ...

  3. XML学习笔记(1)--XML概述

    XML基本概念 XML—extensible Markup Language(可扩展标记语言) XML最基本的三个概念 1)XML语言---描述事物本身(可扩展) 2)XSL语言---展现事物表现形式 ...

  4. 2.6.1 XML配置:创建XML文件

    (1) 工程名右击---New--file  --  newfile窗口中:filename中输入testng.xml testng.xml 文件中打开后,切换到source 标签中.进行编辑. 内容 ...

  5. XML学习——java解析xml文件

    递归获取每个标签 package test; import java.io.File; import java.util.List; import org.dom4j.Document; import ...

  6. XML学习总结(一)——XML介绍

    一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...

  7. XML学习笔记——关于XML解析器

    本篇文章基于W3C而写 在Firefox及其他浏览器中的XML解析器(除IE) var xmlDoc=document.implementation.createDocument("&quo ...

  8. XML学习笔记之XML的简介

    最近,自学了一段时间xml,希望通过学习笔记的整理能够巩固一下知识点,也希望把知识分享给你们(描红字段为重点): XML(extensible Markup language):可扩展的标记语言,解决 ...

  9. C# -- 使用XmlDocument或XDocument创建xml文件

    使用XmlDocument或XDocument创建xml文件 需引用:System.Xml; System.Xml.Linq; 1.使用XmlDocument创建xml(入门案例) static vo ...

随机推荐

  1. 辛巴学院-Unity-剑英陪你零基础学c#系列(一)Hello World

    这不是草稿 辛巴学院:正大光明的不务正业.辛巴学院:攻城狮与荣耀石. 剑英陪你系列又来啦.剑英是一个有大爱的人,热爱每一个程序员,尤其是年轻漂亮的女程序.最近组织朋友们玩了一次即兴团体诗创作,无论怎么 ...

  2. 从点击Button到弹出一个MessageBox, 背后发生了什么

    思考一个最简单的程序行为:我们的Dialog上有一个Button, 当用户用鼠标点击这个Button时, 我们弹出一个MessageBox. 这个看似简单的行为, 谁能说清楚它是如何运行起来的,背后究 ...

  3. 有强迫症的我只能自己写一个json格式化工具

    缘由 为什么博客园的markdown解析出问题了啊?好奇怪啊! 一直以来在编码规范界有2大争论不休的话题,一个是关于是用空格缩进还是tab缩进的问题,一个是花括号是否换行的问题,笔者是tab缩进和花括 ...

  4. js模版引擎handlebars.js实用教程——关于HTML编码

    返回目录 <!DOCTYPE html> <html> <head> <META http-equiv=Content-Type content=" ...

  5. linux下目录操作

    1. 常用函数 #include <dirent.h> //open a directory //failed-NULL, other-return a DIR stream DIR *o ...

  6. 为什么eclipse中启动tomcat后,浏览器中出现404?

    问题描述: tomcat压缩包加压后,启动lib文件夹下面的startup.bat,在浏览器中输入http://localhost:8080/后出现熟悉的界面. 但是在eclipse中,jsp可以正常 ...

  7. Android开发学习之路-使用Handler和Message更新UI

    在Android中,在非主线程中更新UI控件是不安全的,app在运行时会直接Crash,所以当我们需要在非主线程中更新UI控件,那么就需要用到Handler和Message来实现 Demo中,使用到一 ...

  8. Zend Studio导入ThinkPHP工程

    1.一般来说,thinkPHP文件工程(简称php工程)要部署到www下面,那么可以先复制一份php工程到非www文件夹的地方(如桌面): 2.打开zend studio右键,File-New-Loc ...

  9. Android入门(十七)Android多线程

    原文链接:http://www.orlion.ga/670/ 一.在子线程中更新UI Android中不允许在子线程中更新UI,只能在主线程中更新,但是我们有时候必须在子线程中执行一些耗时的任务,然后 ...

  10. 【原创】.NET读写Excel工具Spire.Xls使用(1)入门介绍

    在.NET平台,操作Excel文件是一个非常常用的需求,目前比较常规的方法有以下几种: 1.Office Com组件的方式:这个方式非常累人,微软的东西总是这么的复杂,使用起来可能非常不便,需要安装E ...