關于遍曆XML的問題,請看看 問題是這樣的,我有一個XML文件,想把它每個節點都取出來,比如一個格式化的HTML文件,想按順序遍曆一次,修改其中的內容,現以記錄的形式保存到數據庫,該怎么辦呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果能有建构的话,建议使用反系列化取出你要的各个元素http://blog.csdn.net/cnming/archive/2007/10/11/1819980.aspx 如果没有架构的话,就只能通过遍历了 #region XMLGetElement public static string XMLGetElement(System.Xml.XmlNode pXN, string pElement) { try { string pCmdName = ""; XMLGetElement(pXN, pElement, ref pCmdName); return pCmdName; } catch (NullReferenceException NullEx) { throw NullEx; } catch (Exception Ex) { throw Ex; } } #endregion XMLGetElement public static void XMLGetElement(System.Xml.XmlNode pXN, string pElement, ref string pCmdName) { try { if (pXN.Name == pElement) { pCmdName = pXN.InnerText; return; } foreach (System.Xml.XmlNode mXN in pXN) { if (mXN.ChildNodes.Count > 0) { XMLGetElement(mXN, pElement, ref pCmdName); } } } catch (NullReferenceException NullEx) { throw NullEx; } catch (Exception Ex) { throw Ex; } } 如果文件比较大,使用XmlReader否则XmlDocument也可以具体语法参照MSDN~~~~~ 读取放入一个listboxpublic void ReadXmlAttributes() { this._listBox.Items.Clear(); this.xmlTxtRd = new XmlTextReader(this._xmlPath); try { while(xmlTxtRd.Read()) { if (xmlTxtRd.NodeType == XmlNodeType.Element) { if (xmlTxtRd.HasAttributes) { this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has " + xmlTxtRd.AttributeCount + " Attributes"); this._listBox.Items.Add("The Attributes are:"); while(xmlTxtRd.MoveToNextAttribute()) { this._listBox.Items.Add(xmlTxtRd.Name + " = " + xmlTxtRd.Value); } } else { this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has no Attribute"); } this._listBox.Items.Add(""); } } } catch(XmlException xmlExp) { throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString()); } finally { if (this.xmlTxtRd != null) this.xmlTxtRd.Close(); } } C#中将DateTimePicker控件清空的方法!绝对有效! c# 文件的上传和下载 在C#中使用 aforge.net 的问题 HTML页面报错 有点问题! 当前上下文不存在名称“InitializeComponent”怎么解决啊?! 如何设置数据库的连接时间? 只要答案有效,100分全给你啦!!! 显示图片的问题 现在有什么在.Net程序中使用java代码的好办法的 用jmail组件发送邮件 title显示不出来 为什么啊?急!!!!!!!!!!! 怎样在一个新页面中运行??
http://blog.csdn.net/cnming/archive/2007/10/11/1819980.aspx 如果没有架构的话,就只能通过遍历了 #region XMLGetElement public static string XMLGetElement(System.Xml.XmlNode pXN, string pElement)
{
try
{
string pCmdName = ""; XMLGetElement(pXN, pElement, ref pCmdName); return pCmdName;
}
catch (NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception Ex)
{
throw Ex;
}
} #endregion XMLGetElement public static void XMLGetElement(System.Xml.XmlNode pXN, string pElement, ref string pCmdName)
{
try
{
if (pXN.Name == pElement)
{
pCmdName = pXN.InnerText;
return;
} foreach (System.Xml.XmlNode mXN in pXN)
{
if (mXN.ChildNodes.Count > 0)
{
XMLGetElement(mXN, pElement, ref pCmdName);
}
}
}
catch (NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception Ex)
{
throw Ex;
}
}
否则XmlDocument也可以具体语法参照MSDN~~~~~
public void ReadXmlAttributes()
{
this._listBox.Items.Clear();
this.xmlTxtRd = new XmlTextReader(this._xmlPath); try
{
while(xmlTxtRd.Read())
{
if (xmlTxtRd.NodeType == XmlNodeType.Element)
{
if (xmlTxtRd.HasAttributes)
{
this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has " + xmlTxtRd.AttributeCount + " Attributes"); this._listBox.Items.Add("The Attributes are:"); while(xmlTxtRd.MoveToNextAttribute())
{
this._listBox.Items.Add(xmlTxtRd.Name + " = " + xmlTxtRd.Value);
}
}
else
{
this._listBox.Items.Add("The Element " + xmlTxtRd.Name + " has no Attribute");
}
this._listBox.Items.Add("");
}
}
}
catch(XmlException xmlExp)
{
throw new XmlException(_errMsg + this._xmlPath + xmlExp.ToString());
}
finally
{
if (this.xmlTxtRd != null)
this.xmlTxtRd.Close();
}
}