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;

}

相关推荐
Lucky_Turtle36 分钟前
【Java Xml】Apache Commons Digester3解析
xml·java·apache
cimeo1 小时前
【C 学习】12.2-函数补充
学习·c#
晚枫~4 小时前
零基础快速上手Playwright自动化测试
javascript·python·测试工具·c#·自动化
爱编程的鱼5 小时前
C# 变量详解:从基础概念到高级应用
java·算法·c#
莫陌尛.5 小时前
xml 方式声明式事务案例
xml
cimeo6 小时前
【C学习】13-数组使用与运算
学习·c#
c#上位机7 小时前
wpf之Interaction.Triggers
c#·wpf
FuckPatience8 小时前
前端Vue 后端ASP.NET Core WebApi 本地调试交互过程
前端·vue.js·asp.net
是木子啦11 小时前
wpf passwordbox控件 光标移到最后
c#·wpf
布伦鸽11 小时前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf