当前位置:编程学习 > C#/ASP.NET >>

新手提问!如何在页面中自动获取xml文件中的相应内容?

请问各位大大:
如果有个code.xml结构如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<people>
<code>1010</code>
<name>Tom</name>
<dept>IT</dept>
</people>
<people>
<code>2003</code>
<name>Jack</name>
<dept>HR</dept>
</people>
         ... ...
</record>

而网页上也有对应的code、name、dept三个文本框。
如何实现在页面上手工输入了code后,后面name和dept与之对应的内容能从以上的xml文件中自动获取呢?

小弟新手,能否麻烦详细解释,贴出必要的代码,多谢! --------------------编程问答-------------------- 带名称空间的 XML 的操作使用XmlNamespaceManager   
System.Xml.XmlNamespaceManager nsmanager = new System.Xml.XmlNamespaceManager(doc.NameTable);  
nsmanager.AddNamespace("","");  
System.Xml.XmlNode node = doc.SelectSingleNode( "", nsmanager);  
 http://topic.csdn.net/u/20101201/11/35f25a35-862b-466d-a80a-cfd2d7d46d51.html
 
--------------------编程问答-------------------- 采用ajax方式
<input type ="text"  onkeyup="testAjax(this.value);"/>
testAjax()中用ajax调用后台代码,此代码的功能是查找含有code值的name和dept,
之后返回到前台,对name、dept文本框赋值就行了 --------------------编程问答-------------------- --------------------编程问答--------------------

    public class ReadInfo
    {
        public Record GetRecordInfo(String code,String xmlPath)
        {
            Record r = new Record();
            r.Code = code;
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPath);
            XmlElement e = doc.DocumentElement;
            foreach (XmlNode node in e.ChildNodes)
            {
                if (node.FirstChild.InnerText == code)
                {
                    r.Name = node.ChildNodes[1].InnerText;
                    r.Dept = node.ChildNodes[2].InnerText;
                    break;
                }
            }
            return r;
        }
    }
    public class Record
    {
        public String Code { get; set; }
        public String Name { get; set; }
        public String Dept { get; set; }
    }


   <div>
        <asp:TextBox ID="txtCode" runat="server" ontextchanged="txtCode_TextChanged" AutoPostBack="true"></asp:TextBox>
          <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            <asp:TextBox ID="txtDept" runat="server"></asp:TextBox>
    </div>


 protected void txtCode_TextChanged(object sender, EventArgs e)
        {
            ReadInfo info = new ReadInfo();
            Record r =info.GetRecordInfo(this.txtCode.Text.Trim(),Server.MapPath("Code.xml"));
            this.txtName.Text = r.Name;
            this.txtDept.Text= r.Dept;
        }
--------------------编程问答-------------------- 楼上很详细了。我就不贴代码了。 --------------------编程问答-------------------- --------------------编程问答--------------------  <?xml version="1.0" encoding="UTF-8" ?> 
- <statuses>
- <status>
  <created_at>Sun Jan 01 16:27:21 +0800 2012</created_at> 
  <id>3397063192945352</id> 
  <text>各个国家会突然进入金融街人竟然同看一看</text> 
- <source>
  <a href="http://weibo.com">新浪微博</a> 
  </source>
  <favorited>false</favorited> 
  <truncated>false</truncated> 
  <geo /> 
  <in_reply_to_status_id /> 
  <in_reply_to_user_id /> 
  <in_reply_to_screen_name /> 
  <mid>3397063192945352</mid> 
- <user>
  <id>2643054794</id> 
  <screen_name>BTBTRGRGRG</screen_name> 
  <name>BTBTRGRGRG</name> 
  <province>33</province> 
  <city>1</city> 
  <location>浙江 杭州</location> 
  <description /> 
  <url /> 
  <profile_image_url>http://tp3.sinaimg.cn/2643054794/50/0/1</profile_image_url> 
  <domain /> 
  <gender>m</gender> 
  <followers_count>0</followers_count> 
  <friends_count>27</friends_count> 
  <statuses_count>1</statuses_count> 
  <favourites_count>0</favourites_count> 
  <created_at>Fri Dec 30 00:00:00 +0800 2011</created_at> 
  <following>false</following> 
  <verified>false</verified> 
  <allow_all_act_msg>false</allow_all_act_msg> 
  <geo_enabled>true</geo_enabled> 
  </user>
  </status>
  </statuses>
就这张页面是通过地址远程访问到的远程网站页面,如何将上面的文字保存在本地并且为xml格式的字符串变量,可以供方法读取xml的一下标签元素的值,(如:可以通过text(上文中的<text>各个国家会突然进入金融街人竟然同看一看</text>) 
可以读取text的值-各个国家会突然进入金融街人竟然同看一看),请高手帮忙,我QQ是1071259849 --------------------编程问答-------------------- 我是通过一下地址访问到的页面
"http://api.t.sina.com.cn/statuses/user_timeline.xml?source=602838366&user_id=2643054794"
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,