silverlight 读写XML 根据节点属性删除节点

1.DataConnection.xml



 
 
 
 
 
 

2.在xmal 页面中 给下拉框绑定 xml 中的值----读取

在load 方法中

            string url = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
            //加载model
            WebClient clientmodel = new WebClient();
            var strmodeluri = url.Substring(0, url.LastIndexOf("/")) + "/XML/DataConnection.xml";
            clientmodel.DownloadStringAsync(new Uri(strmodeluri));
            clientmodel.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringModelCompleted);

//读取xml 结束 在client_DownloadStringModelCompleted 中接收返回的值

 void client_DownloadStringModelCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XmlReader reader = XmlReader.Create(new StringReader(e.Result));
            XDocument document = XDocument.Load(reader);
            List stationsModels = new List();
            //***********************************************
            List Sname = new List();
            foreach (var stationname in document.Descendants("SName"))
            {

                Sname.Add(stationname.Attribute("Name").Value);
            }
            Sname.Insert(0, "--请选择--");
            ServerName.ItemsSource = Sname;//给下拉框绑定数据源

            ServerName.SelectedIndex = 0;
        }

 //绑定结束 结果图如下

silverlight 读写XML 根据节点属性删除节点_第1张图片

 

3--写入

(1)因为没有找到更好的方法,只有创建服务,在.net 程序中写入xml,在silverlight页面调用服务

 //点击某个按钮触发调用服务的事件

                    SaveXmlServerClient srvSaveXml = new SaveXmlServerClient();
                    srvSaveXml.Endpoint.Address = new System.ServiceModel.EndpointAddress(App.AppConfig.ServiceUrl + "/Services/SaveXmlServer.svc");//调用服务
                    //***********************************************
                    srvSaveXml.SaveXmlAsync("SName", this.ServerName.SelectedValue.ToString());                   
                    srvSaveXml.SaveXmlCompleted += new EventHandler(srvSaveXml_SaveXmlCompleted);
 //SaveXmlServer.svc 此服务所对应的类 SaveXmlServer.cs  关键代码如下

public void SaveXml(string sname,string value)
 {
        //创建新的XML
        //初始化XML文档操作类
        XmlDocument myXml = new XmlDocument();
        string mappath = Server.MapPath("../XML/DataConnection.xml");//获取xml 转换为物理路径
        //加载指定的XML文件
        myXml.Load(mappath);
        //添加新元素节点 
        XmlElement ele0 = myXml.CreateElement("SName");

//节点中添加属性 name 值为 方法接收的参数 value
        ele0.SetAttribute("Name", value);

//另一个状态属性 state
        ele0.SetAttribute("state", "1");//状态
        //获取根节点
        XmlElement root = myXml.DocumentElement;
        //将新节点存储到元xml 中
        root.AppendChild((XmlNode)ele0);
        //保存所有的修改
        myXml.Save(mappath);
 }

//到此已经对xml 进行了修改。因为silverlight页面属于客户端的,不能在silverlight 页面直接操作保存修改后的xml ,所以只能调用服务,在.net 的项目中进行保存

 4.删除节点(删除节点 SName 当SName的 Name属性=“192.168.1.82”     

 同样在sl 页面中触发调用服务的事件,创建删除xml 的服务

 //删除服务器节点
        private void btn_Del_Click(object sender, RoutedEventArgs e)
        {
            if (this.ServerName.SelectedIndex != 0)
            {
                //调用服务 
                RemoveXmlServiceClient srv = new RemoveXmlServiceClient();
                srv.Endpoint.Address = new System.ServiceModel.EndpointAddress(App.AppConfig.ServiceUrl + "/Services/RemoveXmlService.svc");     //RemoveXmlService.svc 删除节点的服务
                srv.RemoveXmlNodeCompleted += new EventHandler(srv_RemoveXmlNodeCompleted);
                srv.RemoveXmlNodeAsync(this.ServerName.SelectedValue.ToString());   

//this.ServerName.SelectedValue=此例子中将要删除节点中name 属性=192.168.1.82的节点
            }
            else {

                MessageBox.Show("请选择您要删除的服务器!");
            }
        }

//RemoveXmlService.svc 此服务所对应类内容 关键代码

[OperationContract]
    public void RemoveXmlNode(string value)
    {

value=“192.168.1.82”;
        //初始化XML文档操作类
        XmlDocument myXml = new XmlDocument();
        string mappath = Server.MapPath("../XML/DataConnection.xml");
        //加载指定的XML文件
        myXml.Load(mappath);
        //获取根节点
        XmlElement root = myXml.DocumentElement;
        XmlNodeList list = myXml.GetElementsByTagName("SName");
        for (int i = 0; i < list.Count; i++)
        {
            if (list.Item(i).Attributes.GetNamedItem("Name").Value == value)
            {
                root.RemoveChild(list.Item(i));//删除根节点下的满足条件的节点
            }
        }
        //保存所有的修改
        myXml.Save(mappath);
    }

 

//成功删除,删除后的 xml 结果



 
 
 
   

转载于:https://www.cnblogs.com/shenhu-aihua/archive/2012/02/10/2345131.html

你可能感兴趣的:(silverlight 读写XML 根据节点属性删除节点)