我想动态的创建一个XML文件.
例如:<?xml version="1.0" encoding="utf-8"?>
<root imageWidth='440' imageHeight='250'>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=1" frame="_blank" imageUrl="images/01.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=2" frame="_blank" imageUrl="images/02.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=3" frame="_blank" imageUrl="images/03.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=4" frame="_blank" imageUrl="images/04.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=5" frame="_blank" imageUrl="images/05.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=6" frame="_blank" imageUrl="images/06.jpg"/>
<menu url="http://localhost:9671/Web/WorksDetail.aspx?id=7" frame="_blank" imageUrl="images/07.jpg"/>
</root>取出的?id=(这里想动态的去从数据库里取)imageUrl=(这里也是)
用了这种方法// 创建一个 XML 文档
        XmlDocument xmlDoc = new XmlDocument();
        // 添加XML声明
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "", "yes");
        xmlDoc.PrependChild(xmlDeclaration);
        // 添加根元素
        XmlElement nodeElement = xmlDoc.CreateElement("root");
        xmlDoc.AppendChild(nodeElement);
        XmlElement xe1 = xmlDoc.CreateElement("menu");
        xmlDoc.DocumentElement.AppendChild(xe1);
        xmlDoc.Save("D:\\Yiranhunsha\\Web\\xml\\image.xml");可是CreateElement("root imageWidth='440' imageHeight='250'") 这么加就会报:“名称中不能包含“ ”字符(十六进制值 0x20)。”
请高手给解答一下.谢谢

解决方案 »

  1.   

                XDocument Document = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement("root",
                        new XAttribute("imageWidth","440"),
                        new XAttribute("imageHeight","250")));
                foreach (var v in Enumerable.Range(1, 10))
                {
                    Document.Element("root").Add(new XElement("menu",
                        new XAttribute("url", String.Format("http://localhost:9671/Web/WorksDetail.aspx?id={0}",v)),
                        new XAttribute("frame", "_blank"),
                        new XAttribute("imageUrl", String.Format("images/{0}.jpg",v))));            }
                Document.Save("c:\\txt.xml");
      

  2.   

    CreateElement(@"root imageWidth='440' imageHeight='250'")试一下
      

  3.   

    using System.Xml.Linq;
    using System.Linq;
      

  4.   

    哦,看了你的xml文件,这样弄不行。            XmlDocument xmlDoc = new XmlDocument();
                // 添加XML声明
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "", "yes");
                xmlDoc.PrependChild(xmlDeclaration);
                // 添加根元素
                XmlElement nodeElement = xmlDoc.CreateElement("root");
                xmlDoc.AppendChild(nodeElement);
                 nodeElement.SetAttribute("imageWidth", "'440'");
                nodeElement.SetAttribute("imageHeight", "'250'");            XmlElement xe1 = xmlDoc.CreateElement("menu");
              
                for (int i=1 ;i<10;i++)
                {
                    
                   string url = "http://localhost:9671/Web/WorksDetail.aspx?id=" + i.ToString();
                     
                   string imgurl =string.Format("images/0{0}.jpg",i);
                    xe1.SetAttribute("url",url);
                    xe1.SetAttribute("frame", "_blank");
                    xe1.SetAttribute("imageUrl", imgurl);
                }
                xmlDoc.DocumentElement.AppendChild(xe1);
                xmlDoc.Save("E:\\image.xml");        xmlDoc.Save("D:\\Yiranhunsha\\Web\\xml\\image.xml");
      

  5.   

    失误!!!,没放进循环内
                XmlDocument xmlDoc = new XmlDocument();
                // 添加XML声明
                XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "", "yes");
                xmlDoc.PrependChild(xmlDeclaration);
                // 添加根元素
                XmlElement nodeElement = xmlDoc.CreateElement("root");
                xmlDoc.AppendChild(nodeElement);
                nodeElement.SetAttribute("imageWidth", "'440'");
                nodeElement.SetAttribute("imageHeight", "'250'");
                for (int i = 1; i < 10; i++)
                {
                    XmlElement xe1 = xmlDoc.CreateElement("menu");
                    string url = "http://localhost:9671/Web/WorksDetail.aspx?id=" + i.ToString();
                    string imgurl = string.Format("images/0{0}.jpg", i);
                    xe1.SetAttribute("url", url);
                    xe1.SetAttribute("frame", "_blank");
                    xe1.SetAttribute("imageUrl", imgurl);
                    xmlDoc.DocumentElement.AppendChild(xe1);
                }
    xmlDoc.Save("D:\\Yiranhunsha\\Web\\xml\\image.xml");         
      

  6.   

    这个是根据你的例子改的,其实现在常使用的是1楼的方法。linq to xml
      

  7.   

    XML完全操作