<?xml version='1.0' encoding='UTF-8'?>
<tupian>
        <tupian filename="a0.jpg"  label="0商店名zi" url="http://www.baidu.com.cn" />        
</tupian>
我想用c#生成这种格式的xml文件,请大侠们帮忙!
我现在只能生成 <tupian filename="a0.jpg"/>   这种格式的,tupian标签里只有一项!

解决方案 »

  1.   

    可以参考一下这个 XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.NewLineOnAttributes = true;
                XmlWriter writer = XmlWriter.Create("e:\\bppknew.xml", settings);
                writer.WriteStartDocument();
                writer.WriteStartElement("book");
                writer.WriteAttributeString("gnere", "Mystery");
                writer.WriteAttributeString("publicationdate", "2001");
                writer.WriteAttributeString("ISBN", "123456789");
                writer.WriteElementString("title", "Case of the missing Cookie");
                writer.WriteStartElement("author");
                writer.WriteAttributeString("name", "Cookie Monster");
                writer.WriteEndElement();
                writer.WriteElementString("price", "9.99");
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();这是生成的xml文件,含多个属性的<?xml version="1.0" encoding="utf-8"?>
    <book
      gnere="Mystery"
      publicationdate="2001"
      ISBN="123456789">
      <title>Case of the missing Cookie</title>
      <author
        name="Cookie Monster" />
      <price>9.99</price>
    </book>
      

  2.   

    XmlDocument xmldoc = new XmlDocument();  
    XmlNode xmlnode = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);  
    xmldoc.AppendChild(xmlnode);  XmlElement xmlelem = xmldoc.CreateElement("", "", "");  
    XmlText xmltext = xmldoc.CreateTextNode("");  
    xmlelem.AppendChild(xmltext);  
    xmldoc.AppendChild(xmlelem);  

    xmldoc.LoadXml("");
    还有XMLTextWrite
      

  3.   

    用这个代码   ,我最近就写这样格式的XML
    XmlTextWriter writer = new XmlTextWriter(Application.StartupPath + "\\test.xml", null);
                     writer.Formatting = Formatting.Indented;//书写根元素
                     writer.WriteStartElement("tupian");   
                          writer.WriteStartElement("tupian");
                          writer.WriteStartAttribute("filename");
                          writer.WriteString("a0.jpg");
                          writer.WriteStartAttribute("label");
                          writer.WriteString('0商店名zi");
                          writer.WriteStartAttribute("url");
                          writer.WriteString(="http://www.baidu.com.cn");                                 //writer.
                                 
                          writer.WriteEndAttribute();
                          writer.WriteEndElement();
                           
                              
                   
                     writer.WriteFullEndElement();
                     writer.Close();
      

  4.   

    .net 中xml类,你继承下就可以了
    一般都是先建文档,创建节点、属性再创建子节点、属性
    基本比较简单。
    熟悉基本的后给你一个参考封装类
    XML操作类,包括读取/插入/修改/删除。

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;namespace PuTianCheng
    {
        /// <summary>
        /// XmlHelper 的摘要说明
        /// </summary>
        public class XmlHelper
        {
            public XmlHelper()
            {
            }        /// <summary>
            /// 读取数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
            /// <returns>string</returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Read(path, "/Node", "")
             * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
             ************************************************/
            public static string Read(string path, string node, string attribute)
            {
                string value = "";
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
                }
                catch { }
                return value;
            }        /// <summary>
            /// 插入数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
            /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
             * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
             ************************************************/
            public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                            xe.InnerText = value;
                        else
                            xe.SetAttribute(attribute, value);
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 修改数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
             ************************************************/
            public static void Update(string path, string node, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Delete(path, "/Node", "")
             * XmlHelper.Delete(path, "/Node", "Attribute")
             ************************************************/
            public static void Delete(string path, string node, string attribute)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xn.ParentNode.RemoveChild(xn);
                    else
                        xe.RemoveAttribute(attribute);
                    doc.Save(path);
                }
                catch { }
            }
        }
    }==================================================XmlFile.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <Root />==================================================使用方法:string xml = Server.MapPath("XmlFile.xml");
    //插入元素
    //XmlHelper.Insert(xml, "/Root", "Studio", "", "");
    //插入元素/属性
    //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室");
    //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香鱼工作室");
    //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "谱天城工作室");
    //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Master", "", "红尘静思");
    //插入属性
    //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/");
    //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "", "Url", "http://www.luckfish.net/");
    //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "", "Url", "http://www.putiancheng.com/");
    //修改元素值
    //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']/Master", "", "RedDust");
    //修改属性值
    //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Url", "http://www.putiancheng.net/");
    //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='谱天城工作室']", "Name", "PuTianCheng Studio");
    //读取元素值
    //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>");
    //读取属性值
    //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>");
    //读取特定属性值
    //Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香鱼工作室']", "Url") + "</div>");
    //删除属性
    //XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url");
    //删除元素
    //XmlHelper.Delete(xml, "/Root/Studio", "");