请问,怎么样在ASP.NET 2.0后台操作XML文件,改变图片的名字?

解决方案 »

  1.   

    MSDN,using System.Xml;Look Demo;
      

  2.   

    你的图片路径是保存在xml的吗
    要修改xml也要知道你的xml的结构啊
    一般步骤是:
    实例化一个XmlDocument
    然后调用Load加载你的xml文件
    然后用SelectSingleNode方法,参数为一个xpath字符串
    然后就可以调用Replace什么的方法就可以修改了
    最后Save下
      

  3.   


    1, 用 XMLDOCUMENT , 网上一大把
    2,反序列话
      

  4.   

    你的XML结构是什么啊?图片的名字存在XML里吗?
      

  5.   

    xpath找到指定的节点后,然后看你的xml结构改。
      

  6.   

    System.Xml
    里面包含的两个类XmlReader和XmlDocument都可以帮你实现要求
      

  7.   

    假如你的图片XML是
    abc.xml<?xml version="1.0" standalone="yes"?>
    <picture>
      <name>图片1</name>
    </picture>再定义一个数据文件
    txt.xml<?xml version="1.0" encoding="gb2312" standalone="yes" ?>
    <picture></picture>
    则先获取图片名称,方法如下
    首先引入
    using System.Xml;
    //全局变量,图片名称
    string picname="";
    public void getXml()
        {
               //获取XML
                string weacherhtml = String.Empty;
                HttpWebRequest rt = null;
                HttpWebResponse rs = null;
                Stream stream = null;
                StreamReader sr = null;
                try
                {
                    rt = (HttpWebRequest)WebRequest.Create("XML路径"+"abc.xml");
                    rs = (HttpWebResponse)rt.GetResponse();
                    stream = rs.GetResponseStream();
                    sr = new StreamReader(stream, System.Text.Encoding.Default);
                    weacherhtml = sr.ReadToEnd();
                }
                catch (Exception ee)
                {
                    throw ee;
                }
                finally
                {
                    sr.Close();
                    stream.Close();
                    rs.Close();
                }
                try
                {
                    //截取图片名称
                    int nameStart = weacherhtml.IndexOf("<name>");
                    int nameEnd = weacherhtml.IndexOf("</name>");
                    picname= weacherhtml.Substring(nameStart , nameEnd - nameStart).Replace("<name>", "");  //获得图片名字
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
        }
    picname就是从XML中获取的图片名称
    然后重新给这个picname赋值,就是赋予更换后的名字,然后再写入abc.xml,方法如下
    //假如给图片1改名为图片2,则picname="图片2";public void writeXML()
        {        XmlDocument doc = new XmlDocument();
            XmlTextReader reader = new XmlTextReader(Server.MapPath(".") + "\\XML\\txt.xml"); //txt.xml路径
            doc.Load(reader);        XmlElement message;   //定义一个节点变量        XmlNode root = doc.DocumentElement;        Random rnd = new Random();        //写入XML
            message = doc.CreateElement("name");
            message.InnerText = picname;
            root.AppendChild(message);        XmlTextWriter xmlWriter = new XmlTextWriter(Server.MapPath(".") + "\\XML\\abc.xml", null); //abc.xml路径
            xmlWriter.Formatting = Formatting.Indented;
            try
            {
                doc.Save(xmlWriter);
            }
            catch
            {
                Response.Write("读写错误");
                Response.End();
                return;
            }        xmlWriter.Close();
            reader.Close();
        }
    这样图片就改变名字了,我想这就是你需要的,你自己再改改,方法就这样^oo^
      

  8.   

    我写的一个xml新闻系统的源代码希望对你有用一.添加数据:public bool AddArticle(string NewsTitle, string NewsContent, string NewsClassID)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(HttpContext.Current.Server.MapPath(articlePath)); //装载文章xml
        int newID = 1;
        if (doc.DocumentElement.SelectSingleNode("//Article[@ID]") != null)
        {
            //最后一个文章ID+1就是新的文章ID
            newID = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("//Article[last()]").Attributes["ID"].Value) + 1;
        }
        XmlElement el = doc.CreateElement("Article");
        XmlAttribute id = doc.CreateAttribute("ID");
        id.Value = newID.ToString();
        XmlAttribute title = doc.CreateAttribute("Title");
        title.Value = NewsTitle;
        XmlAttribute date = doc.CreateAttribute("Date");
        date.Value = DateTime.Now.ToString();
        XmlAttribute classID = doc.CreateAttribute("ClassID");
        classID.Value = NewsClassID;
        XmlCDataSection content = doc.CreateCDataSection(NewsContent);    el.Attributes.Append(id);
        el.Attributes.Append(title);
        el.Attributes.Append(classID);
        el.Attributes.Append(date);
        el.AppendChild(content);
        doc.DocumentElement.AppendChild(el);
        doc.Save(HttpContext.Current.Server.MapPath(articlePath));
        return true;
    }
    二.修改数据public bool EditArticle(string NewsTitle, string NewsContent, string NewsID)
    {
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
            XmlNode node = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
            if (node != null)
            {
                node.Attributes["Title"].Value = NewsTitle;
                node.FirstChild.Value = NewsContent;
            }
            document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
            return true;
        }
        catch
        {
            return false;
        }
    }
    三.删除数据public bool DeleteArticle(string NewsID)
    {
        bool flag = false;
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath(this.articlePath));
            XmlNode oldChild = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
            if (oldChild != null)
            {
                oldChild.ParentNode.RemoveChild(oldChild);
            }
            document.Save(HttpContext.Current.Server.MapPath(this.articlePath));
        }
        catch
        {
            flag = false;
        }
        return flag;
    }
    很旧前写的了,应该比较简单易懂的