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;

}

相关推荐
kaikaile19956 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
wearegogog12311 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃11 小时前
WPF值转换器
c#
摇滚侠12 小时前
Spring 零基础入门到进阶 基于 XML 管理 Bean 14-28
xml·数据库·spring
Popeye-lxw13 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312913 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee15 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路15 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11115 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101116 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel