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;

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐