public class XmlManager
{
private XmlDocument m_XMLDoc = null;
public XmlManager(XmlDocument xmldoc)
{
m_XMLDoc = xmldoc;
}
public XmlManager(string path)
{
m_XMLDoc = new XmlDocument();
m_XMLDoc.Load(path);
}
//读取指定节点下的指定子节点的text
public string GetNodeText(string rootNodeName, string nodeName, string chileNodeName)
{
string value = "";
try
{
XmlNodeList nodes = m_XMLDoc.SelectSingleNode(rootNodeName).ChildNodes;
foreach (XmlNode node in nodes)
{
if (node.LocalName != nodeName)
continue;
XmlNode item = node.SelectSingleNode(chileNodeName);
if (item != null)
{
value = item.InnerText;
}
break;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
return value;
}
////设置指定节点下的指定子节点的text
public void SaveNodeText(string rootNodeName, string nodeName, string chileNodeName, string value, string savePath)
{
try
{
XmlNodeList nodes = m_XMLDoc.SelectSingleNode(rootNodeName).ChildNodes;
foreach (XmlNode node in nodes)
{
if (node.LocalName != nodeName)
continue;
XmlNode item = node.SelectSingleNode(chileNodeName);
if (item != null)
{
item.InnerText = value;
m_XMLDoc.Save(savePath);
}
break;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}