创建XML文件
比如我有这么一段string类型的字符串,我怎么把他写成xml文件中
<?xml version='1.0' encoding='utf-8'?>
<category name='coopcomname'>
<property name='001' value='标题1'/>
<property name='002' value='标题2'/>
------解决方案-------------------- File.WriteAllText("d:\123.xml",str_xml)
------解决方案--------------------例如下面的xml文件:
- <album name="album1">
- <Preview path="album1" extension="xpi" sizew="680" sizeh="474" totalpage="25">
- <Page id="0" text="封面" bgimg="fm.xpi" sizew="680" sizeh="474" totalphoto="2">
<Photo id="0" x="0" y="0" minw="5029" minh="3504" pwidth="680" pheight="474" img="hard_cover_color.xpi" />
<Photo id="1" x="138" y="117" minw="3155" minh="1925" pwidth="406" pheight="243" img="sampleS.xpi" />
</Page>
</Preview>
</album>
using System;
using System.Xml;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument(); // 创建dom对象
XmlElement root = doc.CreateElement("album"); // 创建根节点album
root.SetAttribute("name", "album1"); // 设置属性
doc.AppendChild(root); // 加入到xml document
XmlElement preview = doc.CreateElement("Preview"); // 创建preview元素
preview.SetAttribute("path", "album1"); //
preview.SetAttribute("extension", "xpi"); //
preview.SetAttribute("sizew", "680"); // 设置属性
preview.SetAttribute("sizeh", "474"); //
preview.SetAttribute("totalpage", "25"); //
root.AppendChild(preview); // 添加到xml document
//下面一样,不一行行写解释了
XmlElement page = doc.CreateElement("Page");
page.SetAttribute("id", "0");
page.SetAttribute("text", "封面");
page.SetAttribute("bgimg", "fm.xpi");
page.SetAttribute("sizew", "680");
page.SetAttribute("sizeh", "474");
page.SetAttribute("totalphoto", "2");
preview.AppendChild(page);
XmlElement photo1 = doc.CreateElement("Photo");
photo1.SetAttribute("id", "0");
photo1.SetAttribute("x", "0");
photo1.SetAttribute("y", "0");
photo1.SetAttribute("minw", "5029");
photo1.SetAttribute("minh", "3504");
photo1.SetAttribute("pwidth", "680");
photo1.SetAttribute("pheight", "474");
photo1.SetAttribute("image", "hard_cover_color.xpi");
page.AppendChild(photo1);
XmlElement photo2 = doc.CreateElement("Photo");
photo2.SetAttribute("id", "1");
photo2.SetAttribute("x", "138");
photo2.SetAttribute("y", "117");
photo2.SetAttribute("minw", "3155");
photo2.SetAttribute("minh", "1925");