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;

}

相关推荐
zjnlswd1 小时前
C#学习笔记4
笔记·学习·c#
曹牧3 小时前
C#:List<string>特定格式输出
windows·c#
Z5998178414 小时前
c#软件开发学习笔记--Modbus-TCP/UDP网口通讯
笔记·学习·c#
曹牧5 小时前
C#:取的多个药品批准文号中的一个
c#
用户298698530148 小时前
3 种方法,轻松将 PowerPoint 转换为 PDF 格式
人工智能·后端·c#
KindSuper_liu9 小时前
HybridCLR DHE及MetaVersion工作流核心概念
c#
淡海水9 小时前
01-08-运行时-虚方法分派与接口调用的底层实现
linux·windows·microsoft·c#·虚方法·vtable·vsd
李高钢9 小时前
C# WPF Prism 进阶(三):导航(Navigation)深入
开发语言·c#·wpf
我是苏苏10 小时前
C#基础:不写for循环的五种方式
数据结构·算法·c#
six_1 天前
C# 上位机(持续更新中)
前端·面试·c#