简单的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("*******************");
}
}
}
}
}
问题请看代理里面“???????”处