Silverlight中使用xml文件(xml文件保存,节点的添加,删除)

我的xml文件

ExpandedBlockStart.gif 代码
<? xml version="1.0" encoding="utf-8"  ?>
< Pushpins >
  
< Pushpin >
    
< ID > bcab6565-f1c7-4ee3-88a1-ab3115112ca5 </ ID >
    
< Title > nn </ Title >
    
< Description > mmmm </ Description >
    
< VELatLongLatitude > 37.814344816897488 </ VELatLongLatitude >
    
< VELatLongLongitude > 112.31771841645201 </ VELatLongLongitude >
  
</ Pushpin >
  
< Pushpin >
    
< ID > 641f277b-da9c-4291-832f-700777a6c9b5 </ ID >
    
< Title > www </ Title >
    
< Description > wwwww </ Description >
    
< VELatLongLatitude > 34.4796226939071 </ VELatLongLatitude >
    
< VELatLongLongitude > 108.88998404145201 </ VELatLongLongitude >
  
</ Pushpin >
</ Pushpins >

 

在silverlight页面中添加引用

using System.Xml;
using System.Xml.Linq;

 

使用 LINQ to XML 从任意 URI 位置加载 XML 文件,代码如下

ExpandedBlockStart.gif 代码
  string  uriString  =   " ../XMLData/XMLMapsPushpin.xml " ;
        XDocument xDoc 
=   null ;
        
// 使用 LINQ to XML 从任意URI 位置加载 XML 文件
         private   void  LoadXmlData()
        {
            WebClient wc 
=   new  WebClient();
            wc.OpenReadCompleted 
+=  wc_OpenReadCompleted;
            wc.OpenReadAsync(
new  Uri(uriString, UriKind.Relative));
        }
        
private   void  wc_OpenReadCompleted( object  sender, OpenReadCompletedEventArgs e)
        {
            
if  (e.Error  !=   null )
            {
                
// OutputTextBlock.Text = e.Error.Message;
                 return ;
            }
            
using  (Stream s  =  e.Result)
            {
                xDoc 
=  XDocument.Load(s);

                
this .spPushpin.Children.Clear();
                
foreach  (var c  in  xDoc.Descendants( " Pushpin " ))
                {
                    
// 添加图钉到地图上 PushPinControl 是一个自定义控件
                    PushPinControl pushpin  =   new  PushPinControl();
                    pushpin.Title 
=  c.Element( " Title " ).Value;
                    pushpin.Remark 
=  c.Element( " Description " ).Value;
                    pushpin.Tag 
=  c.Element( " ID " ).Value;
                    
double  x  = double .Parse(c.Element( " VELatLongLatitude " ).Value);
                    
double  y  =   double .Parse(c.Element( " VELatLongLongitude " ).Value);
                    myMapLayer.AddChild(pushpin, 
new  Location(x, y));
                }
            }
        }

 

删除xml文件中的节点,代码如下:

ExpandedBlockStart.gif 代码
private   void  DelXmlElement ()
{
IEnumerable
< XElement >  qurey  =
                    from item 
in  xDoc.Root.Elements()
                    
where  item.Element( " ID " ).Value  == " aaa "
                    select item;
                qurey.Remove();
}

 

 

xml文件中添加节点:

ExpandedBlockStart.gif 代码
private   void  AddXmlElement()
{
if (xDoc == null  )
                {                  
                    xDoc 
=   new  XDocument(  new  XDeclaration( " 1.0 " " utf-8 " " yes " ),
                                                    
new  XElement( " Pushpins " ));
                }
                
if  (xDoc  !=   null )
                {
                    XElement newPushpin 
=   new  XElement( " Pushpin " ,
                            
new  XElement( " ID " ,ID),
                            
new  XElement( " Title " , pushpin.Title),
                            
new  XElement( " Description " , pushpin.Remark),
                            
new  XElement( " VELatLongLatitude " , CurrentLocation.Latitude),
                            
new  XElement( " VELatLongLongitude " , CurrentLocation.Longitude)     
                       );
                    xDoc.Root.Add(newPushpin);                   
                }
 }

 

保存xml文档到服务器指定的位置:

ExpandedBlockStart.gif 代码
#region  保存xml文档到服务器
        
private   void  Button_SaveXml( object  sender, RoutedEventArgs e)
        {
            WebClient webClient 
=   new  WebClient();
            webClient.OpenWriteCompleted 
+=   new  OpenWriteCompletedEventHandler(webClient_OpenWriteCompleted);
            webClient.OpenWriteAsync(
new  Uri( " /WebSaveXmlFile.aspx?filename=XMLMapsPushpin.xml " ,UriKind.Relative));
        }
        
void  webClient_OpenWriteCompleted( object  sender, OpenWriteCompletedEventArgs e)
        {
            Stream outputStream 
=  e.Result;
            
string  str  =   " <?xml version=\ " 1.0 \ "  encoding=\ " utf - 8 \ "  ?> " ;
            
string  outString  =  str  +  xDoc.ToString();
            
byte [] fileContent  =  Encoding.UTF8.GetBytes(outString);            
            outputStream.Write(fileContent, 
0 , fileContent.Length);
            
// No need to flush.
            outputStream.Close();
            MessageBox.Show(
" 保存成功! " );
        }
        
#endregion  保存xml文档到服务器

 

服务器端asp.net页面代码:

ExpandedBlockStart.gif 代码
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
string  fileName  =  Request.Params[ " filename " ];
            
if  ( ! string .IsNullOrEmpty(fileName))
            {
                Stream inputStream 
=  Request.InputStream;
                FileStream fileStream 
=  File.Create(Server.MapPath( " ~ " +   " \\XMLData\\ "   +  fileName);
                
byte [] fileContent  =   new   byte [inputStream.Length];
                inputStream.Read(fileContent, 
0 , fileContent.Length);
                fileStream.Write(fileContent, 
0 , fileContent.Length);
                fileStream.Flush();
                fileStream.Close();
                inputStream.Close();
                
// No need to respond.
                Response.Clear();
                Response.End();
            }
        }

 

 

 

 

转载于:https://www.cnblogs.com/z_lb/archive/2010/05/06/1729155.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinUI 控件、UWP 控件、WPF 控件和 Silverlight 控件在语法和结构上有所不同,因此可以通过检查 XAML 代码的命名空间来区分它们。以下是一些常见的命名空间和控件: - WinUI 控件:命名空间为 `http://schemas.microsoft.com/winui/2021/xaml/behaviors` 或 `http://schemas.microsoft.com/winui/2021/xaml/presentation`,控件名称以 `Microsoft.UI` 开头。 - UWP 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/winfx/2008/xaml/presentation`,控件名称以 `Windows.UI` 开头。 - WPF 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/netfx/2007/xaml/presentation`,控件名称以 `System.Windows` 或 `Microsoft.Windows` 开头。 - Silverlight 控件:命名空间为 `http://schemas.microsoft.com/winfx/2006/xaml/presentation` 或 `http://schemas.microsoft.com/client/2007`,控件名称以 `System.Windows.Controls` 或 `Microsoft.Windows.Controls` 开头。 可以通过读取 XAML 文件的命名空间来确定使用的控件类型。例如,以下代码片段演示了如何读取 XAML 文件的命名空间: ```csharp using System.Xml.Linq; // Load XAML file into an XDocument XDocument xdoc = XDocument.Load("MyXamlFile.xaml"); // Get the root element of the XAML file XElement root = xdoc.Root; // Get the default namespace of the XAML file XNamespace ns = root.GetDefaultNamespace(); // Check the namespace to determine the type of controls used in the XAML file if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winui")) { // WinUI controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/winfx")) { // UWP or WPF controls } else if (ns.NamespaceName.StartsWith("http://schemas.microsoft.com/client")) { // Silverlight controls } else { // Unknown namespace } ``` 请注意,这只是一种简单的方法来区分不同类型的控件,实际上还需要考虑一些其他因素,例如控件的属性和行为。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值