silverlight xml查询

因为对linq不熟,所以不想用linq查询,所以用最土的方法,如果有好方法的话,还请告之.

xml文件

 

代码
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
    
<book genre='Silverlight' publicationdate='12-22-2008' ISBN='978-0470193938'>
        
<title>Professional Silverlight 2</title>
        
<author>
            
<first-name>Mike</first-name>
            
<last-name>Meyers</last-name>
        
</author>
        
<price>49.99</price>
    
</book>
    
<book genre='Silverlight' publicationdate='02-15-2009' ISBN='978-1933988573'>
        
<title>Hacking Silverlight 2</title>
        
<author>
            
<first-name>David</first-name>
            
<last-name>Kelley</last-name>
        
</author>
        
<price>44.99</price>
    
</book>
    
<book genre='ASP.NET' publicationdate='10-20-2008' ISBN='978-0735626218'>
        
<title>Microsoft ASP.NET and AJAX: Architecting Web Applications</title>
        
<author>
            
<first-name>Dino</first-name>
            
<last-name>Esposito</last-name>
        
</author>
        
<price>39.99</price>
    
</book>
</bookstore>

 

 

xmlquery.xaml

 

代码
  <Canvas x:Name="LayoutRoot" Background="White">
        
<TextBlock Text="输入图书类型:" Width="180" Canvas.Left="10" Canvas.Top="10" FontSize="14" />
        
<TextBox x:Name="TextBoxGenre" Width="180" Height="30" FontSize="14"
            Canvas.Left
="10" Canvas.Top="50" />
        
<Button x:Name="ButtonGenre" Content="确定" Width="120" Height="40"
            Canvas.Left
="40" Canvas.Top="100"  Click="ButtonGenre_Click" />
        
<TextBlock Text="输入作者姓名:" Width="180" Canvas.Left="210" Canvas.Top="10" FontSize="14" />
        
<TextBox x:Name="TextBoxAuthor" Width="180" Height="30" FontSize="14"
            Canvas.Left
="210" Canvas.Top="50" />
        
<Button x:Name="ButtonAuthor" Content="确定" Width="120" Height="40"
            Canvas.Left
="240" Canvas.Top="100"  Click="ButtonAuthor_Click"/>
        
<TextBlock x:Name="TextBlockResult" Text="查询结果:" FontSize="14"
            Canvas.Left
="10" Canvas.Top="150" TextWrapping="Wrap" />
    
</Canvas>

 

xmlquery.xaml.cs

 

代码
XmlReader reader;
        
string author;
        
public xmlquery()
        {
            InitializeComponent();
            DownloadFile();
        }
        
private void ButtonGenre_Click(object sender, RoutedEventArgs e)
        {
            
string genre = TextBoxGenre.Text;           
            
while (reader.Read())
            {
                
if (reader.ReadToFollowing("book"))
                {
                    reader.MoveToAttribute(
"genre");
                    reader.GetAttribute(
"genre");
                    
string strgenre = reader.Value;
                    
if (strgenre == genre)
                    {
                        
if (reader.ReadToFollowing("title"))
                        {
                            
string title=    reader.ReadElementContentAsString();
                            TextBlockResult.Text 
+= "\n书名:" + title;
                        }                        
                    }
                }
            }
            reader.Close();
        }
        
private void RequestProceed(IAsyncResult asyncResult)
        {
            HttpWebRequest request 
= asyncResult.AsyncState as HttpWebRequest;
            Stream requestStream 
= request.EndGetRequestStream(asyncResult);
            
using (StreamWriter writer = new StreamWriter(requestStream))
            {
                writer.Write(
"author=" + author);
                writer.Flush();
            }
            request.BeginGetResponse(
new AsyncCallback(ResponseProceed), request);
        }
        
void ResponseProceed(IAsyncResult asyncResult)
        {
            HttpWebRequest request 
= asyncResult.AsyncState as HttpWebRequest;
            HttpWebResponse response 
= request.EndGetResponse(asyncResult) as HttpWebResponse;
            Stream responseStream 
= response.GetResponseStream();
            StreamReader reader 
= new StreamReader(responseStream);
            Action
<StreamReader> showInfo = this.ShowBookInfo;
            
this.Dispatcher.BeginInvoke(showInfo, reader);
        }
        
void ShowBookInfo(StreamReader reader)
        {
            TextBlockResult.Text 
= "查询结果:\n" + reader.ReadToEnd();
        }

        
private void ButtonAuthor_Click(object sender, RoutedEventArgs e)
        {
            author 
= TextBoxAuthor.Text;
            Uri address 
= new Uri("http://localhost:4995/BookStoreHandler.ashx");
            HttpWebRequest request 
= WebRequest.Create(address) as HttpWebRequest;
            request.Method 
= "POST";
            request.ContentType 
= "application/x-www-form-urlencoded";
            request.BeginGetRequestStream(
new AsyncCallback(RequestProceed), request);

        }
        
private void DownloadFile()
        {
            WebClient client 
= new WebClient();
            Uri address 
= new Uri("http://localhost:4995/bookstore.xml");
            client.DownloadStringCompleted 
+= new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(address);

        }
        
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            StringReader xmlString 
= new StringReader(e.Result);
            reader 
= XmlReader.Create(xmlString);

        }

BookStoreHandler.cs

 

代码
public void ProcessRequest(HttpContext context)
        {

            
//string genre = context.Request.Form["genre"].ToString();
            
//string result = GetBookInfo(genre);

            
string author = context.Request.Form["author"].ToString();
            
string result = GetBookInfobyname(author);
            context.Response.Write(result);
            
        }
 
private string GetBookInfobyname(string name)
        {
            StringBuilder builder 
= new StringBuilder();

            XmlDocument document 
= new XmlDocument();
            document.Load(
"http://localhost:4995/bookstore.xml");
            XmlNodeList xmlnodelist 
= document.SelectNodes("bookstore/book");
            
foreach (XmlNode xmlnode in xmlnodelist)
            {
                
string title = xmlnode.SelectSingleNode("author/last-name").InnerText.Trim();
                
if (title == name)
                {
                    
                    builder.Append(
"书名:");
                    builder.Append(xmlnode.SelectSingleNode(
"title").FirstChild.Value);
                    builder.Append(
"价格:");
                    builder.Append(xmlnode.SelectSingleNode(
"price").FirstChild.Value);
                }
                

            }
            
return builder.ToString();
        }

 

 

 

 

posted on 2010-06-13 14:42  叶子绿了  阅读(306)  评论(0编辑  收藏  举报

导航