我发送了个请求,返回如下XML信息<?xml version="1.0" encoding="UTF-8"?>
  <status>
    <created_at>Fri Mar 25 10:25:22 +0800 2011</created_at>
    <id>4444343</id>
    <text>文本数据。。</text>
    <source>
      <a href="http://open.t.sina.com.cn">新浪</a>
    </source>
    <favorited>false</favorited>
    <truncated>false</truncated>
    <in_reply_to_status_id></in_reply_to_status_id>
    <in_reply_to_user_id></in_reply_to_user_id>
    <in_reply_to_screen_name></in_reply_to_screen_name>
    <mid>2134545305989</mid>
 <geo xmlns:georss="http://www.georss.org/georss">
      <georss:point>0 0</georss:point>
    </geo>
    <user>
      <id>1833453533</id>
      <screen_name>е޽v</screen_name>
      <name>е޽v</name>
      <province>44</province>
      <city>3</city>
      <location>㶫 </location>
      <description>描述</description>
      <url>http://1</url>
      <profile_image_url>http://tp2.sinaimg.cn/1831642233/50/1300871975/1</profile_image_url>
      <domain></domain>
      <gender>m</gender>
      <followers_count>132</followers_count>
      <friends_count>683</friends_count>
      <statuses_count>381</statuses_count>
      <favourites_count>0</favourites_count>
      <created_at>Sat Oct 16 00:00:00 +0800 2010</created_at>
      <following>false</following>
      <verified>false</verified>
      <allow_all_act_msg>false</allow_all_act_msg>
      <geo_enabled>true</geo_enabled>
    </user>
  </status>
-------------------------------------------
我用如下方法 DataSet ds = new DataSet();        string result = oauth.oAuthWebRequest(oAuthSina.Method.POST, url, "status=" + HttpUtility.UrlEncode(statusText.Text));
        this.resultTextBox2.Text = result;
        System.IO.StringReader stream = new System.IO.StringReader(result); 
System.Xml.XmlTextReader   xr   =   new  System.Xml.XmlTextReader(stream);       
ds.ReadXml(xr);
Response.Write(ds.Tables.Count.ToString());输出的结果为:5 意思是包含了5个表。现在我只想取 <status> 下的id,text,以及<user> 下的id,name.....等。如何处理?

解决方案 »

  1.   

        DataTable dt = ds.Tables[1];
       
      

  2.   

    根据表名取,这种方法似乎不通用,顺序没法知道
    能不能通过处理XML来取呢?然后重新组合起来,填到新的DS里
      

  3.   

    直接用xmldocument来处理,更方便高效。
      

  4.   

    用xmldocument,xmldocument.seletSingleNode/seletNodes,提取各个节点,递归一下就可以了。
      

  5.   

    XML操作类
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;namespace PuTianCheng
    {
        /// <summary>
        /// XmlHelper 的摘要说明
        /// </summary>
        public class XmlHelper
        {
            public XmlHelper()
            {
            }        /// <summary>
            /// 读取数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
            /// <returns>string</returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Read(path, "/Node", "")
             * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
             ************************************************/
            public static string Read(string path, string node, string attribute)
            {
                string value = "";
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
                }
                catch { }
                return value;
            }        /// <summary>
            /// 插入数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
            /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
             * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
             ************************************************/
            public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                            xe.InnerText = value;
                        else
                            xe.SetAttribute(attribute, value);
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 修改数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
             ************************************************/
            public static void Update(string path, string node, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Delete(path, "/Node", "")
             * XmlHelper.Delete(path, "/Node", "Attribute")
             ************************************************/
            public static void Delete(string path, string node, string attribute)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xn.ParentNode.RemoveChild(xn);
                    else
                        xe.RemoveAttribute(attribute);
                    doc.Save(path);
                }
                catch { }
            }
        }
    }
    调用看了自己组合一下