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;

}

相关推荐
王维志26 分钟前
Unity Embedded Browser文档翻译
unity·c#
FlYFlOWERANDLEAF31 分钟前
DevExpress中Word Processing Document API学习记录
学习·c#·word
棉晗榜1 小时前
C#写字符串到Modbus中
c#·modbus
MoFe18 小时前
【.net core】【watercloud】登陆后跳转至指定页面,显示在系统框架页内
.netcore
周杰伦fans8 小时前
.net core webapi/mvc阿里云服务器部署 - 错误解决
阿里云·mvc·.netcore
Hello.Reader10 小时前
从零到一上手 Protocol Buffers用 C# 打造可演进的通讯录
java·linux·c#
浪扼飞舟11 小时前
c#基础(一)
开发语言·c#
Luna-player12 小时前
某个工程 pom.xml
xml
一个帅气昵称啊13 小时前
C# .NET EFCore 性能优化
性能优化·c#·.net
ArabySide13 小时前
【C#】理解.NET内存机制:堆、栈与装箱拆箱的底层逻辑及优化技巧
c#