Silverlight对Xml增删改查(Linq To Xml)

Silverlight不支持C#操作Xml那一套。
只能使用Linq To Xml.
感觉不如C#操作Xml那么强大。
还是Xpath语法强大一些吧。

ExpandedBlockStart.gif 大气象
< UserControl  x:Class ="SilverlightOperateXml.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable
="d"
    d:DesignHeight
="300"  d:DesignWidth ="400" >

    
< Grid  x:Name ="LayoutRoot"  Background ="Black"  Width ="400"  Height ="300" >
        
< TextBlock  Height ="246"  HorizontalAlignment ="Left"  Margin ="12,12,0,0"  Name ="txtXml"  Text ="TextBlock"  VerticalAlignment ="Top"  Width ="373"  Foreground ="White"   />
        
< Button  Content ="增"  Height ="23"  HorizontalAlignment ="Left"  Margin ="10,264,0,0"  Name ="btnAdd"  VerticalAlignment ="Top"  Width ="75"  Click ="btnAdd_Click"   />
        
< Button  Content ="删"  Height ="23"  HorizontalAlignment ="Left"  Margin ="110,264,0,0"  Name ="btnDelete"  VerticalAlignment ="Top"  Width ="75"  Click ="btnDelete_Click"   />
        
< Button  Content ="改"  Height ="23"  HorizontalAlignment ="Left"  Margin ="210,264,0,0"  Name ="btnUpdate"  VerticalAlignment ="Top"  Width ="75"  Click ="btnUpdate_Click"   />
        
< Button  Content ="查"  Height ="23"  HorizontalAlignment ="Left"  Margin ="310,264,0,0"  Name ="btnQuery"  VerticalAlignment ="Top"  Width ="75"  Click ="btnQuery_Click"   />
    
</ Grid >
</ UserControl >
ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

using  System.Xml;
using  System.Xml.Linq; // 需要手工添加引用
using  System.Text;
using  System.IO;

namespace  SilverlightOperateXml
{
    
public   partial   class  MainPage : UserControl
    {
        
public  MainPage()
        {
            InitializeComponent();
            
// 初始化
             this .Loaded  +=   new  RoutedEventHandler(Page_Loaded);
        }

        
// 第一次加载
         void  Page_Loaded( object  sender, RoutedEventArgs e)
        {
            txtXml.Text 
=
                
@" <?xml version=""1.0"" encoding=""utf-16"" ?>
                <items>
                    <item id=""1""></item>
                    <item id=""2""></item>
                </items>
" ;
        }
        
// Linq实体类
         public   class  Item
        {
            
public   int  id {  get set ; }
        }
        
#region  增 删 改 查

        
//
         private   void  xmlAdd( string  xmlStr, Item item)
        {
            TextReader txtReader 
=   new  StringReader(xmlStr);
            XDocument xDoc 
=  XDocument.Load(txtReader);
            XElement xitem 
=   new  XElement( " item " );
            xitem.SetAttributeValue(
" id " " 0 " );
            
// xitem.SetElementValue("value", "hi");
            xDoc.Root.AddFirst(xitem);

            txtXml.Text 
=  xDoc.ToString();
        }
        
//
         private   void  xmlDelete( string  xmlStr,  string  id)
        {
            TextReader txtReader 
=   new  StringReader(xmlStr);
            XDocument xDoc 
=  XDocument.Load(txtReader);

            var item 
=  from p  in  xDoc.Descendants( " item " )
                       
where  p.Attribute( " id " ).Value  ==  id
                       select p;
            item.Remove();

            txtXml.Text 
=  xDoc.ToString();
        }
        
//
         private   void  xmlUpdate( string  xmlStr,  string  id, string  value)
        {
            TextReader txtReader 
=   new  StringReader(xmlStr);
            XDocument xDoc 
=  XDocument.Load(txtReader);

            IEnumerable
< XElement >  qurey  =
                                    from item 
in  xDoc.Descendants( " item " )
                                    
where  item.Attribute( " id " ).Value  ==  id
                                    select item;
            XElement xel 
=  qurey.FirstOrDefault();

            
if  (xel  ==   null return ;
            xel.Attribute(
" id " ).Value  =   " 002 " ;
            xel.SetElementValue(
" value " , value);

            txtXml.Text 
=  xDoc.ToString();
        }
        
//
         private   void  xmlQuery( string  xmlStr)
        {
            TextReader txtReader 
=   new  StringReader(xmlStr);
            XDocument xDoc 
=  XDocument.Load(txtReader);

            var items 
=  from f  in  xDoc.Descendants( " item " )
                        select 
new  Item
                           {
                               id 
=   int .Parse(f.Attribute( " id " ).Value)
                           };
            List
< Item >  _items  =   new  List < Item > ();
            _items.AddRange(items);

            
// 遍历
             for  ( int  i  =   0 ; i  <  _items.Count; i ++ )
            {
                MessageBox.Show(_items[i].id.ToString());
            }
        }

        
#endregion

        
private   void  btnAdd_Click( object  sender, RoutedEventArgs e)
        {
            Item item 
=   new  Item();
            item.id 
=   0 ;
            xmlAdd(txtXml.Text, item);
        }

        
private   void  btnDelete_Click( object  sender, RoutedEventArgs e)
        {
            xmlDelete(txtXml.Text, 
" 1 " );
        }

        
private   void  btnUpdate_Click( object  sender, RoutedEventArgs e)
        {
            xmlUpdate(txtXml.Text, 
" 2 " " hello " );
        }

        
private   void  btnQuery_Click( object  sender, RoutedEventArgs e)
        {
            xmlQuery(txtXml.Text);
        }
    }
}

 

源码:http://files.cnblogs.com/greatverve/SilverlightOperateXml.rar

转载于:https://www.cnblogs.com/greatverve/archive/2010/11/10/silverlight-xml.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值