c#设置xml内容不换行及属性xsi:nil=true的空节点添加

所属分类: 网络编程 / ASP.NET 阅读数: 1633
收藏 0 赞 0 分享
1.设置生成xml的内容格式为不换行
默认用下面代码创建并生成xml的代码如下:
复制代码 代码如下:

XmlDocument doc = new XmlDocument();
//这里为创建节点等代码,省略....
//保存
doc.Save(filename);
结果生成的节点有换行:
<UserName>
</UserName>

这样的话会导致xsd中如果有验证会通不过,要想不换行,doc.Save(filename);可以改为:
复制代码 代码如下:

using (XmlTextWriter xtw = new XmlTextWriter(filename, null))
{
//None表示不应用特殊格式,另一个相反枚举值Indented表示缩进
xtw.Formatting = Formatting.None;
doc.Save(xtw);
}

2.添加属性为xsi:nil="true"的空节点
复制代码 代码如下:

public static XmlElement CreateNodeWithNullAttr(XmlDocument doc, string nodeName)
{
XmlElement element = doc.CreateElement(nodeName);
XmlAttribute attr = doc.CreateAttribute("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance");
attr.Value = "true";
element.SetAttributeNode(attr);
//element.Attributes.Append(attr);
return element;
}
更多精彩内容其他人还在看

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享

ASP.NET入门随想之检票的老太太

ASP.NET入门随想之检票的老太太
收藏 0 赞 0 分享
查看更多