大家好,请教一个读xml的问题。我读的时候发生异常。
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<mmk:manifesto>
<!-- Server Test -->
<mmk:application name="BXYZ001">
<mmk:module name="CBXYZ001_a.exe" ver="20060105120001"/>
<mmk:module name="CBXYZ001_a.exe.config" ver="20060105120001"/>
<mmk:module name="CBXYZ001_001.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_002.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_003.dll" ver="20060105120001"/>
</mmk:application>
</mmk:manifesto>我的程序:(简单写写)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace XMLReader
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string strProRunPath = Application.ExecutablePath;
string strXmlPath = strProRunPath.Substring(0, strProRunPath.LastIndexOf("\\") + 1);
string strXmlFullName = strXmlPath + "Manifest.xml";
string strNode = @"mmk:manifesto/mmk:application";
string strAddStr = string.Empty; XmlDocument objXDoc = new XmlDocument();
objXDoc.Load(strXmlFullName); XmlNodeList objXnl = objXDoc.SelectNodes(strNode); foreach (XmlNode objXNode in objXnl)
{
foreach (XmlNode objXn in objXNode.ChildNodes)
{
strAddStr = objXn.Attributes["name"].ToString() + " : " + objXn.Attributes["ver"].ToString();
listBox1.Items.Add(strAddStr);
}
}
}
}
}
xml文件
<?xml version="1.0" encoding="UTF-8"?>
<mmk:manifesto>
<!-- Server Test -->
<mmk:application name="BXYZ001">
<mmk:module name="CBXYZ001_a.exe" ver="20060105120001"/>
<mmk:module name="CBXYZ001_a.exe.config" ver="20060105120001"/>
<mmk:module name="CBXYZ001_001.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_002.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_003.dll" ver="20060105120001"/>
</mmk:application>
</mmk:manifesto>我的程序:(简单写写)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;namespace XMLReader
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
string strProRunPath = Application.ExecutablePath;
string strXmlPath = strProRunPath.Substring(0, strProRunPath.LastIndexOf("\\") + 1);
string strXmlFullName = strXmlPath + "Manifest.xml";
string strNode = @"mmk:manifesto/mmk:application";
string strAddStr = string.Empty; XmlDocument objXDoc = new XmlDocument();
objXDoc.Load(strXmlFullName); XmlNodeList objXnl = objXDoc.SelectNodes(strNode); foreach (XmlNode objXNode in objXnl)
{
foreach (XmlNode objXn in objXNode.ChildNodes)
{
strAddStr = objXn.Attributes["name"].ToString() + " : " + objXn.Attributes["ver"].ToString();
listBox1.Items.Add(strAddStr);
}
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<Manifest xmlns:mmk='urn:samples'>
<mmk:manifesto>
<!-- Server Test -->
<mmk:application name="BXYZ001">
<mmk:module name="CBXYZ001_a.exe" ver="20060105120001"/>
<mmk:module name="CBXYZ001_a.exe.config" ver="20060105120001"/>
<mmk:module name="CBXYZ001_001.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_002.dll" ver="20060105120001"/>
<mmk:module name="CBXYZ001_003.dll" ver="20060105120001"/>
</mmk:application>
</mmk:manifesto>
</Manifest>代码改为这样:
string strProRunPath = Application.ExecutablePath;
string strXmlPath = strProRunPath.Substring(0, strProRunPath.LastIndexOf("\\") + 1);string strXmlFullName = strXmlPath + "Manifest.xml";
string strNode = @"/Manifest/mmk:manifesto/mmk:application";
string strAddStr = string.Empty;XmlDocument objXDoc = new XmlDocument();
objXDoc.Load(strXmlFullName);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(objXDoc.NameTable);
nsmgr.AddNamespace("mmk", "urn:samples");//加入namespaceXmlNodeList objXnl = objXDoc.SelectNodes(strNode,nsmgr);foreach (XmlNode objXNode in objXnl)
{
foreach (XmlNode objXn in objXNode.ChildNodes)
{
strAddStr = objXn.Attributes["name"].Value.ToString() + " : " + objXn.Attributes["ver"].Value.ToString();
ListBox1.Items.Add(strAddStr);
}
}