c# 关于XML的增删改查
public static string GetXmlNodeToString(string parentnode, string childNode)
{
string ReturnMessage = string.Empty;
string path = @"C:\config.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNode parentroot = xmlDoc.SelectSingleNode("xxxx");
XmlNode root = parentroot.SelectSingleNode(parentnode);
XmlNode node = root.SelectSingleNode(childNode);
if (string.IsNullOrEmpty(node.InnerText))
{
ReturnMessage = "ConfigXML节点为空";
return ReturnMessage;
}
return node.InnerText;
}
public static void UpdateNode(string parentnode, string childNode, string nodeValue)
{
string path = @"C:\config.xml";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNode parentroot = xmlDoc.SelectSingleNode("xxx");
XmlNode root = parentroot.SelectSingleNode(parentnode);
XmlNode node = root.SelectSingleNode(childNode);
node.InnerText = nodeValue;
xmlDoc.Save(path);
}
public static DataSet XmlConversionToClass(string xmlString)
{
DataSet ds = new DataSet();
StringReader stream = new StringReader(xmlString);
XmlTextReader reader = new XmlTextReader(stream);
ds.ReadXml(reader);
return ds;
}