惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Last Watchdog
The Last Watchdog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Y
Y Combinator Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
博客园_首页
小众软件
小众软件
I
InfoQ
J
Java Code Geeks
月光博客
月光博客
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
V
Visual Studio Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Cloudflare Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Latest news
Latest news
G
GRAHAM CLULEY
IT之家
IT之家
C
Cisco Blogs
Last Week in AI
Last Week in AI
Engineering at Meta
Engineering at Meta
L
LangChain Blog
The Register - Security
The Register - Security
SecWiki News
SecWiki News
M
MIT News - Artificial intelligence
NISL@THU
NISL@THU
T
Tenable Blog
博客园 - Franky
美团技术团队
I
Intezer
U
Unit 42
雷峰网
雷峰网
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 叶子绿了

freemarker显示含有html代码的内容 .net sql 防注入 httpmodule [转载]poi导出excel,可以自定义保存路径 jquery ajax 传递js对象到后台 Struts2下多文件的上传与下载 dwr框架 Oracle 9i中包含Connect by 子句的查询向Oracle 10g移植后运行时错误及解决方法 JQuery Uploadify 基于JSP的无刷新上传实例 写了个用jquery控制select只读(select选项可以供用户查看但不能改变初始选中值) C#之模态窗口关闭 repeater相同行合并 在Web站点中创建和使用Rss源(动态) silverlight 3 数据绑定及分页 解决ASP.NET中Image控件不能自动刷新 解决UpdatePanel无法直接弹出窗口的问题 导入Excel数值读不到,找不到可安装的 ISAM错误! asp.net2.0 上传大容量文件第三方控件radupload 浏览器不能正常解析CSS代码的解决 ajax在用户注册中的应用,类似淘宝网
silverlight xml查询
叶子绿了 · 2010-06-13 · via 博客园 - 叶子绿了

因为对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();
        }