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>
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 > sender, RoutedEventArgs e)
{
xmlQuery(txtXml.Text);
}
}
}
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 > sender, RoutedEventArgs e)
{
xmlQuery(txtXml.Text);
}
}
}
源码:https://files.cnblogs.com/greatverve/SilverlightOperateXml.rar