<?xml version="1.0" encoding="utf-8"?>
<ProScore>
  <Pro ID="1">
    <P_ID>1</P_ID>
    <Service>2</Service>
    <Speed>3</Speed>
    <Trust>4</Trust>
  </Pro>
</ProScore>
  <Pro ID="1">
    <P_ID>1</P_ID>这两个值“1” 都是一样的。
现在要根据这个值来修改其他的三个子节点,修改之后保存。需要代码,谢谢!

解决方案 »

  1.   

    传过来4个值,ID为1,Service为5,Speed为6,Trust为7。
    根据ID=1,修改后应该为:
    <?xml version="1.0" encoding="utf-8"?>
    <ProScore>
      <Pro ID="1">
        <P_ID>1</P_ID>
        <Service>5</Service>
        <Speed>6</Speed>
        <Trust>7</Trust>
      </Pro>
    </ProScore>
      

  2.   

    XmlNodeList sectionNodeList = SelectNodes(path,"/configuration ction[@key='" + sectionkey + "']/item");
      

  3.   

    //修改xml就是先删除节点,然后添加新节点 
    XmlDocument xdoc = new XmlDocument();
            xdoc.Load(System.Web.HttpContext.Current.Server.MapPath("文件路径"));
            XmlElement info = xdoc.DocumentElement;        
                
                foreach (XmlNode var in info.ChildNodes)
                {
                    if (var.Attributes["ID"].InnerText=="1")
                    {
                        info.RemoveChild(var);
                    }               
                }            //创建新的Pro   添加到根节点中
                    XmlElement eUser = xdoc.CreateElement("pro");                                    XmlElement eName = xdoc.CreateElement("P_ID");
                    eName.InnerText =id;
                    eUser.AppendChild(eName);                XmlElement RealName = xdoc.CreateElement("Service");
                    RealName.InnerText = a1;
                    eUser.AppendChild(RealName);                XmlElement eEmail = xdoc.CreateElement("Speed");
                    eEmail.InnerText =a2;
                    eUser.AppendChild(eEmail);                XmlElement eState = xdoc.CreateElement("Trust");
                    eState.InnerText = a3;
                    eUser.AppendChild(eState);                info.AppendChild(eUser);
               
            
            XmlTextWriter xtw = new XmlTextWriter(System.Web.HttpContext.Current.Server.MapPath("emailInfo.xml"), Encoding.UTF8);
            xdoc.WriteContentTo(xtw);
            xtw.Close();
      

  4.   


               // 编辑某个节点
                 XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(Server.MapPath("你xml的地址"));
                //获得节点列表
                 XmlNodeList xmlNodeList = xmlDoc.SelectNodes("ProScore[P_ID='1']");//查找
                  XmlNode xmlNode = xmlNodeList.Item(0);
                xmlNode["Service"].InnerText = "5";
                xmlNode["Speed"].InnerText = "6";
                xmlNode["Trust"].InnerText = "7";
                xmlDoc.Save(Server.MapPath(("你xml的地址"));
      

  5.   


    //获得节点列表
    XmlNodeList xmlNodeList = xmlDoc.SelectNodes("ProScore/Pro[id='1'][P_ID='1']");//查找获取节点这地方。你查查。。表达式。。
      

  6.   

    0.0!不是插入啊。是修改!!!
    <ProScore>
        <P_ID>1</P_ID>
        <Service>5</Service>
        <Speed>6</Speed>
        <Trust>7</Trust>
    </ProScore>
    -。-!这样的我会改-。-!!你那样的没改过啊。
      

  7.   

    <ProScore>
      <Pro ID="1">
        <P_ID>1</P_ID>
        <Service>2</Service>
        <Speed>3</Speed>
        <Trust>4</Trust>
      </Pro>
    </ProScore>比你的多了一层。。
      

  8.   

    <ProScore> 
      <Pro_ID>1</Pro> 
        <P_ID>1 </P_ID> 
        <Service>2 </Service> 
        <Speed>3 </Speed> 
        <Trust>4 </Trust> 
      
    </ProScore> 你这样不行吗?
      

  9.   


    XmlNodeList xmlNodeList = xmlDoc.SelectNodes("表达式");只要你表达式写对了就OK了
      

  10.   


        string XMLFile = Server.MapPath("1.xml"); // XML文件地址
        XDocument XML = XDocument.Load(XMLFile); // 需要 System.Xml.Linq 命名空间    List<XElement> a = ( from x in XML.Element("ProScore").Elements("Pro") where x.Attribute("ID").Value=="1" select x ).ToList<XElement>();
        if (a.Count > 0)
        {
            a[0].Element("Service").Value = "5";
            a[0].Element("Speed").Value = "6";
            a[0].Element("Trust").Value = "7";
        }
        XML.Save(XMLFile);
      

  11.   


    <?xml version="1.0" encoding="utf-8"?> 
    <bcaster> 
      <item id="79" item_url="PicNews/Img/u=404630538,2075277077" link="HTML/050/AI_20081017_50_53_79.html" itemtitle="111111111111111111" /> 
      <item id="78" item_url="PicNews/Img/Index_04_01_06.jpg" link="HTML/050/AI_20081017_50_53_78.html" itemtitle="zengjia" /> 
      <item id="77" item_url="PicNews/Img/bsxwf.jpg" link="HTML/050/AI_20081017_50_53_77.html" itemtitle="中国药科大学研究生部" /> 
      <item id="76" item_url="PicNews/Img/江宁大门.jpg" link="HTML/050/AI_20081017_50_53_76.html" itemtitle="研究生部图片新闻" /> 
      <item id="75" item_url="PicNews/Img/中国药科大学校标(完美2).jpg" link="HTML/050/AI_20081017_50_53_75.html" itemtitle="新闻测试图片新闻" /> 
    </bcaster>
    添加节点的函数:/// 将图片新闻信息写入到图片新闻播放器的XML文件集中 
        /// </summary> 
        /// <param name="picpath">图片路径</param> 
        /// <param name="htmlpath">图片新闻网址</param> 
        /// <param name="title">标题</param> 
        public void WritePicNewsXML(string picpath, string htmlpath, string title,string aid) 
        { 
            XmlDocument xmlDoc; 
            xmlDoc = new XmlDocument(); 
            xmlDoc.Load(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));         XmlNodeList xnl = xmlDoc.SelectSingleNode("bcaster").ChildNodes; 
            //if (xnl.Count <= 5)//保持首页图片新闻不超过5条 
            //{ 
                XmlNode rootnode = xmlDoc.SelectSingleNode("bcaster");             XmlElement fel = (XmlElement)rootnode.FirstChild; 
                 
                XmlElement el = xmlDoc.CreateElement("item");//添加子节点及属性 
                el.SetAttribute("id", aid); 
                el.SetAttribute("item_url", picpath); 
                el.SetAttribute("link", htmlpath); 
                el.SetAttribute("itemtitle", title); 
                rootnode.PrependChild(el);//将新加入的图片新闻添加到第一个位置 
                if (xnl.Count > 5) 
                { 
                    XmlNode lxn = rootnode.LastChild; 
                    rootnode.RemoveChild(lxn);//删除最末尾的图片新闻 
                } 
                             xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml")); 
            //} 
        }
    修改XML节点的属性的函数:/// <summary> 
        /// 修改XML属性 
        /// </summary> 
        /// <param name="picpath"></param> 
        /// <param name="htmlpath"></param> 
        /// <param name="title"></param> 
        /// <param name="aid"></param> 
        public void EditPicNewsXML(string picpath, string htmlpath, string title, string aid) 
        {         XmlDocument xmlDoc; 
            xmlDoc = new XmlDocument(); 
            xmlDoc.Load(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));         XmlNodeList xnl = xmlDoc.SelectSingleNode("bcaster").ChildNodes; 
            foreach (XmlNode xn in xnl) 
            { 
                XmlElement xe = (XmlElement)xn; 
                if (xe.GetAttribute("id") == aid)//如果节点存在则改写 
                { 
                    xe.SetAttribute("item_url", picpath); 
                    xe.SetAttribute("link", htmlpath); 
                    xe.SetAttribute("itemtitle", title); 
                    break; 
                } 
            }         xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml")); 
        }
    删除指定的XML节点的函数: /// <summary> 
        /// 删除XML指定节点 
        /// </summary> 
        /// <param name="aid"></param> 
        public void DelPicNewsXML(string aid) 
        { 
            XmlDocument xmlDoc; 
            xmlDoc = new XmlDocument(); 
            xmlDoc.Load(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));         XmlNodeList xnl = xmlDoc.SelectSingleNode("bcaster").ChildNodes; 
            foreach (XmlNode xn in xnl) 
            { 
                XmlElement xe = (XmlElement)xn; 
                if (xe.GetAttribute("id") == aid)//如果节点存在删除 
                { 
                    xe.RemoveAll(); 
                    break; 
                } 
            }         xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml")); 
        }
    试试看
      

  12.   

     // 编辑某个节点
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("test1.xml");
                //获得节点
                XmlNode node = xmlDoc.SelectSingleNode("/ProScore/Pro[@ID=1]");
                if (node != null)
                {
                    node["Service"].InnerText = "5";
                    node["Speed"].InnerText = "6";
                    node["Trust"].InnerText = "7";
                }
                xmlDoc.Save("test1.xml");