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

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

大气象
<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>
大气象
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 { getset; }
        }
        
#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 == nullreturn;
            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);
        }
    }
}

 

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

posted @ 2010-11-10 09:03  大气象  阅读(2438)  评论(16编辑  收藏  举报
http://www.tianqiweiqi.com