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;

}

相关推荐
江沉晚呤时5 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
Oberon5 小时前
Avalonia硬配.NET Framework 4.8
c#·.net·avalonia·.net framework
FreeBuf_6 小时前
黄金旋律IAB组织利用暴露的ASP.NET机器密钥实施未授权访问
网络·后端·asp.net
喵叔哟8 小时前
3. 【Blazor全栈开发实战指南】--Blazor是什么?为什么选择Blazor?
c#·.netcore
钢铁男儿11 小时前
C# 接口(接口可以继承接口)
java·算法·c#
超级小忍11 小时前
在 Spring Boot 中使用 MyBatis 的 XML 文件编写 SQL 语句详解
xml·spring boot·mybatis
你我约定有三15 小时前
spring--xml注入时bean的property属性
xml·java·spring
小码编匠18 小时前
C# 的西门子数控系统 OPCUA 数据采集开发从零入门
后端·数据分析·c#
孜然卷k18 小时前
C#项目 在Vue/React前端项目中 使用使用wkeWebBrowser引用并且内部使用iframe网页外链 页面部分白屏
前端·vue.js·react.js·c#
专注VB编程开发20年19 小时前
C# VB.NET多进程-管道通信,命名管道(Named Pipes)
开发语言·c#·.net