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;

}

相关推荐

  1. C#拼接xml

    2024-05-25 18:10:36       11 阅读
  2. C#字符串的拼接

    2024-05-25 18:10:36       8 阅读
  3. C# —— 字符串拼接

    2024-05-25 18:10:36       8 阅读
  4. xml怎么用【C#,XML

    2024-05-25 18:10:36       12 阅读
  5. C/C++蓝桥杯之整数拼接(较难)

    2024-05-25 18:10:36       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-25 18:10:36       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-25 18:10:36       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-25 18:10:36       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-25 18:10:36       18 阅读

热门阅读

  1. xmlhttp中withcredential用法

    2024-05-25 18:10:36       11 阅读
  2. 使用HTTP客户端在Python中进行网页抓取——笔记

    2024-05-25 18:10:36       14 阅读
  3. Vue2常用的组件通信方式有几种

    2024-05-25 18:10:36       15 阅读
  4. STL源码看书笔记(1)——代码解析

    2024-05-25 18:10:36       9 阅读
  5. Qt TreeWidget详细说明

    2024-05-25 18:10:36       12 阅读
  6. Golang:使用net/http实现一个简易的http服务器

    2024-05-25 18:10:36       11 阅读
  7. Go 实现程序优雅退出

    2024-05-25 18:10:36       8 阅读
  8. Python数据类型转换

    2024-05-25 18:10:36       9 阅读
  9. UDP服务器与客户端之间的区别?

    2024-05-25 18:10:36       10 阅读