我有一个简单的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("*******************");
}
}
}
}}问题请看代理里面“???????”处
---------------------------------
但这个怎么读取呢?
{
//这里怎样读取oth,oth2的值?????
listBox1.Items.Add("*******************");
}
-----------------------------
其实在这个地方只要可以获取XmlNode xn = objNdRd.????
或者
XmlNode xn = (XmlNode)objNdRd;
这样就可以把问题解答了。谁知道怎么办?????????
http://www.1jq.net/sample/xmldemo.aspx
http://weekzero.cnblogs.com/articles/178140.html