读:
public static string GetNodeString(XmlDocument doc, string xpath)
{
string nodeValue = "";
XmlElement root = doc.DocumentElement;
XPathNavigator nav = root.CreateNavigator();
XPathNodeIterator xpNodIter = nav.Select(xpath);
if (xpNodIter.MoveNext()) {
nodeValue = xpNodIter.Current.ToString();
}
return nodeValue;
}
写:
private static bool setNodeValue(XmlDocument doc, String path, String nodeValue)
{
if(doc == null || labelType == "" || labelType == null ) return false;
XmlNode node = doc.SelectSingleNode(path);
XmlNode valueNode = node.NextSibling;
if(valueNode != null)
{
if( nodeValue == null || "".Equals(nodeValue))
{
valueNode.InnerText = "";
}
else
{
valueNode.InnerText = text;
}
doc.Save();
return true;
}
else
return false;
}
博客给出了XML节点读写的代码示例。读操作通过给定的XPath获取节点字符串值;写操作根据路径设置节点值,若节点存在则更新其文本内容,最后保存文档,若参数不合法则返回失败。

被折叠的 条评论
为什么被折叠?



