C#拼接xml

1、xml字符串示例

<?xml version="1.0" encoding="utf-8" standalone="no"?><DATA><ITEMS><ITEM><ID>01<ID/><CODE>0001<CODE><NAME>测试1<NAME/></ITEM></ITEMS></DATA>

2、方法

private string Getxml()

{

XmlDocument document = new XmlDocument();

XmlDeclaration xd = document.CreateXmlDeclaration("1.0", "utf-8", "");

XmlElement root = null;

document.AppendChild(document.CreateElement("DATA"));

root = document.DocumentElement;

document.InsertBefore(xd, root);

XmlElement items = document.CreateElement("ITEMS");

XmlElement item = document.CreateElement("ITEM");

items .AppendChild(item);

XmlElement id = document.CreateElement("ID");

item.AppendChild(id);

XmlElement code = document.CreateElement("CODE");

item.AppendChild(code);

XmlElement code = document.CreateElement("NAME");

item.AppendChild(name);

root.AppendChild(items);

string xmlData = document.OuterXml;

//存xml

if (!string.IsNullOrEmpty(xmlData))

{

XmlDocument doc = new XmlDocument();

doc.LoadXml(xmlData);

string filepath = "C:\\Test\\test.xml";

doc.Save(filepath);

}

return xmlData;

}

相关推荐
葬歌倾城1 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
Eiceblue3 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王3 小时前
hello判断
开发语言·c#
金增辉5 小时前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
future14127 小时前
C#学习日记
开发语言·学习·c#
傻啦嘿哟8 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
deriva9 小时前
.netcore+ef+redis+rabbitmq+dotcap先同步后异步再同步的方法,亲测有效
redis·rabbitmq·.netcore
唐青枫13 小时前
C#.NET log4net 详解
c#·.net
Nemo_XP18 小时前
HttpHelper类处理两种HTTP POST请求
c#
lijingguang1 天前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#