简单的XML操作求教(本人努力过了,但没有完成)

Dong 2006-05-12 05:24:32

我有一个简单的XML文件(书里的例子进行小修改),准备对他操作!
XML文件内容:
<?xml version="1.0" encoding="utf-8" ?>
<messages>
<message id="7836733" target="Loreto" status="sent">
<sender>Steven</sender>
<others>
<oth>ccc</oth>
<oth2><![CDATA[111]]></oth2>
</others>
</message>
<message id="7836712" target="Eileen" status="pending">
<sender>Lt</sender>
<others>
<oth>aaa</oth>
<oth2><![CDATA[222]]></oth2>
</others>
</message>
</messages>

处理代码:
private void button4_Click(object sender, System.EventArgs e)
{
string strFilePath = @"D:\temp\DataBase4\XMLFile4.xml";
listBox1.Items.Clear();
XmlDocument document = new XmlDocument();
document.Load(strFilePath);
XmlNodeList objXList = document.SelectNodes("//message[@status]");
foreach(XmlNode objNode in objXList)
{
XmlNodeReader objNdRd = new XmlNodeReader(objNode);

while(objNdRd.Read())
{
if(objNdRd.NodeType == XmlNodeType.Element)
{
if(objNdRd.Name == "message")
{
listBox1.Items.Add("The message with id " + objNdRd.GetAttribute("id") + "was sent to " + objNdRd.GetAttribute("target") + " and is currently " + objNdRd.GetAttribute("status") + ".");

}
else if(objNdRd.Name == "sender")
{
listBox1.Items.Add("The message was sent by " + objNdRd.ReadString());
listBox1.Items.Add("==================");
}
else if(objNdRd.Name == "others")
{
//这里怎样读取oth,oth2的值?????
listBox1.Items.Add("*******************");
}
}
}
}

}

问题请看代理里面“???????”处
...全文
214 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
WeekZero 2006-05-12
  • 打赏
  • 举报
回复
xml的操作吧

在C#.net中如何操作XML
http://weekzero.cnblogs.com/articles/178140.html
duncansun 2006-05-12
  • 打赏
  • 举报
回复
http://blog.csdn.net/duncansun/archive/2006/05/12/726767.aspx
  • 打赏
  • 举报
回复
xmlDataSouce 建议直接拿这个来用,几乎不用写代码,示例如下


http://www.1jq.net/sample/xmldemo.aspx
Dong 2006-05-12
  • 打赏
  • 举报
回复
else if(objNdRd.Name == "others")
{
//这里怎样读取oth,oth2的值?????
listBox1.Items.Add("*******************");
}
-----------------------------
其实在这个地方只要可以获取

XmlNode xn = objNdRd.????
或者
XmlNode xn = (XmlNode)objNdRd;
这样就可以把问题解答了。谁知道怎么办?????????
真相重于对错 2006-05-12
  • 打赏
  • 举报
回复
innerxml
mary3021 2006-05-12
  • 打赏
  • 举报
回复
up
Dong 2006-05-12
  • 打赏
  • 举报
回复
<oth2><![CDATA[222]]></oth2>
---------------------------------
但这个怎么读取呢?
copine 2006-05-12
  • 打赏
  • 举报
回复
有个属性ChildNodes可以取到所有子节点,然后试试Value属性或者Innertext属性。
canserly 2006-05-12
  • 打赏
  • 举报
回复
用SelectSingleNode找到上一级别的NODE,再根据其子节点的NAME判断取值
copine 2006-05-12
  • 打赏
  • 举报
回复
objNdRd没有getchildnode之类的方法吗?应该也会返回XmlNode类型的子节点的,然后必然有方法取到节点的value.

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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