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;

}

相关推荐
He少年1 小时前
【AI 辅助案例分享】
人工智能·c#·编辑器·ai编程
工程师0073 小时前
栈和堆的概念
c#·栈和堆
不会编程的懒洋洋3 小时前
C# P/Invoke 基础
开发语言·c++·笔记·安全·机器学习·c#·p/invoke
Avalon7123 小时前
Unity3D响应式渲染UI框架UniVue
游戏·ui·unity·c#·游戏引擎
njsgcs4 小时前
solidworks折弯自动标注5 非90度折弯
c#·solidworks
狼与自由4 小时前
clickhouse引擎
clickhouse·c#·linq
wangnaisheng4 小时前
【C#】死锁详解:发生原因、优化解决方案
c#
tiger从容淡定是人生5 小时前
AI替代软件战略(一):从 CCleaner 到 MCP 架构重构 —— TigerCleaner 的工程实践
人工智能·重构·架构·c#·mcp
宝桥南山1 天前
GitHub Models - 尝试一下使用GitHub Models
microsoft·ai·微软·c#·github·.netcore
hixiong1231 天前
C# OpenvinoSharp部署INSID3
开发语言·人工智能·ai·c#·openvinosharp