如何取得XML节点的值

哈哈哈尔滨小子 2011-03-26 01:53:56
我得到了如下的字符串,它是以XML形式表现的。如何取得 retweeted_status 下的 text 的值呢?
-------------
<?xml version="1.0" encoding="UTF-8" ?>
- <statuses>
- <status>
<created_at>Fri Mar 25 16:46:11 +0800 2011</created_at>
<id>3444</id>
<text>文字文字......</text>
- <source>
<a href="http://1">uiououou</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>898989889</mid>
- <user>
<id>7777755</id>
<screen_name>yuiuiu</screen_name>
<name>无脚鸟v</name>
<province>44</province>
<city>3</city>
<location>iuouo</location>
<description>fhgfhhgfhgfh.....</description>
<url>http://1</url>
<profile_image_url>http://1</profile_image_url>
<domain />
<gender>m</gender>
<followers_count>132</followers_count>
<friends_count>683</friends_count>
<statuses_count>388</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>
- <retweeted_status>
<created_at>Fri Mar 25 16:25:40 +0800 2011</created_at>
<id>8040898773</id>
<text>uuuuuuuuuuukljlklkj</text>
- <source>
<a href="http://1">sdgfdsfsdfd</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>6666666767</mid>
- <user>
<id>1645979402</id>
<screen_name>yutu</screen_name>
<name>tyuytu</name>
<province>31</province>
<city>15</city>
<location>上海 浦东新区</location>
<description>gfhfhgfhfh</description>
<url>http://2</url>
<profile_image_url>http://1</profile_image_url>
<domain>ghfghgfh</domain>
<gender>m</gender>
<followers_count>15339</followers_count>
<friends_count>349</friends_count>
<statuses_count>4195</statuses_count>
<favourites_count>0</favourites_count>
<created_at>Fri Mar 26 00:00:00 +0800 2010</created_at>
<following>false</following>
<verified>true</verified>
<allow_all_act_msg>true</allow_all_act_msg>
<geo_enabled>true</geo_enabled>
</user>
- <annotations>
- <annotation class="object">
<cartoon type="boolean">false</cartoon>
</annotation>
</annotations>
</retweeted_status>
<annotations />
</status>
</statuses>
...全文
261 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-03-27
  • 打赏
  • 举报
回复
你看看机器人给你给你回复。
LINQ2XML
ycproc 2011-03-27
  • 打赏
  • 举报
回复
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
private XmlDocument xmlDoc;

//load xml file
private void LoadXml()
{
xmlDoc=new XmlDocument();
xmlDoc.Load(Server.MapPath("User.xml"));
}


//添加节点
private void AddElement()
{

LoadXml();

XmlNode xmldocSelect=xmlDoc.SelectSingleNode("user");

XmlElement el=xmlDoc.CreateElement("person"); //添加person节点
el.SetAttribute("name","风云"); //添加person节点的属性"name"
el.SetAttribute("sex","女"); //添加person节点的属性 "sex"
el.SetAttribute("age","25"); //添加person节点的属性 "age"

XmlElement xesub1=xmlDoc.CreateElement("pass"); //添加person节点的里的节点
xesub1.InnerText="123";//设置文本节点
el.AppendChild(xesub1);
XmlElement xesub2=xmlDoc.CreateElement("Address");
xesub2.InnerText="昆明";//设置文本节点
el.AppendChild(xesub2);

xmldocSelect.AppendChild(el);
xmlDoc.Save(Server.MapPath("user.xml"));


}




//修改节点
private void UpdateElement()
{
LoadXml();
XmlNodeList nodeList=xmlDoc.SelectSingleNode("user").ChildNodes;//获取bookstore节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
if(xe.GetAttribute("name")=="风云")//如果name属性值为“风云”
{
xe.SetAttribute("name","发明");


//如果下面有子节点在下走
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="pass")//如果找到
{
xe2.InnerText="66666";//则修改
break;

}
}

break;
}
}
xmlDoc.Save(Server.MapPath("user.xml"));//保存
}


//删出节点
private void deleteNode()
{

LoadXml();
XmlNodeList xnl=xmlDoc.SelectSingleNode("user").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;

if(xe.GetAttribute("name")=="发明")
{
//xe.RemoveAttribute("name");//删除name属性
// xe.RemoveAll();//删除该节点的全部内容
xe.ParentNode.RemoveChild(xe);

break;
}

}
xmlDoc.Save(Server.MapPath("user.xml"));//保存
}

private void showIt()
{
LoadXml();
XmlNode xn=xmlDoc.SelectSingleNode("user");

XmlNodeList xnl=xn.ChildNodes;

foreach(XmlNode xnf in xnl)
{
XmlElement xe=(XmlElement)xnf;
// Console.WriteLine(xe.GetAttribute("name"));//显示属性值
// Console.WriteLine(xe.GetAttribute("sex"));
//
// XmlNodeList xnf1=xe.ChildNodes;
// foreach(XmlNode xn2 in xnf1)
// {
// Console.WriteLine(xn2.InnerText);//显示子节点点文本
// }

}

}


<?xml version="1.0" encoding="gb2312"?>
<user>
<person>
</person>
<person name="ss" sex="男" age="25">
<pass>123</pass>
<Address>a</Address>
</person>
<person name="ff" sex="女" age="25">
<pass>123</pass>
<Address>g</Address>
</person>
</user>

KeepMoving 2011-03-27
  • 打赏
  • 举报
回复
正则,或者XPATH
huangwenquan123 2011-03-26
  • 打赏
  • 举报
回复
        XmlDocument xml = new XmlDocument();
xml.Load(Server.MapPath("XMLFile.xml"));
XmlNodeList node = xml.SelectNodes("//*/retweeted_status/text");
foreach (XmlNode n in node)
{
Response.Write(n.InnerText);
}
/*
uuuuuuuuuuukljlklkj
*/
stepday 2011-03-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxr0323 的回复:]
XML操作类

C# code
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.W……
[/Quote]

绝对符合你的需求
  • 打赏
  • 举报
回复
"retweeted_status/text" 格式这样?
  • 打赏
  • 举报
回复
那个node参数是什么?


是节点
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wxr0323 的回复:]

XML操作类
C# code
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.We……
[/Quote]

试过了,提示“System.NullReferenceException: 未将对象引用设置到对象的实例。”
那个node参数是什么?如果按我给出那些字符串?
subxli 2011-03-26
  • 打赏
  • 举报
回复
XPATH 查询xml节点
子夜__ 2011-03-26
  • 打赏
  • 举报
回复
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 { }
}
}
}
  • 打赏
  • 举报
回复

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧