C#写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>

那位能帮忙用C#的两个类xmlDocument 和 xmlNode帮我写一下上面上面这个XML文挡.保存在(album文件夹中)

慕哥9229398
浏览 1157回答 2
2回答

森林海

using System;using System.Xml;namespace ConsoleApplication3{class Program{static void Main(string[] args){XmlDocument doc = new XmlDocument(); // 创建dom对象XmlElement root = doc.CreateElement("album"); // 创建根节点albumroot.SetAttribute("name", "album1"); // 设置属性doc.AppendChild(root); // 加入到xml documentXmlElement 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");photo2.SetAttribute("pwidth", "406");photo2.SetAttribute("pheight", "243");photo2.SetAttribute("image", "sampleS.xpi");page.AppendChild(photo2);doc.Save(@"album\album1.xml"); // 保存文件}}}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP