自己写了个xml 操作类 多多指教

zlb789 2009-02-26 04:31:42

XmlDocument doc = null;
System.Xml.XmlNode node;
string fileName = string.Empty;//文件路径+ 名称
private string _errorInfo;//错误的友好信息 -给用户见的
public string ErrorInfo
{
get { return _errorInfo; }
}
#region 构造函数
public XmlHelp(string filep)
{
fileName = filep;
LoadXml();
}
public XmlHelp(string fileP,string root,string content)
{
fileName = fileP;
CreateSampleXmlFile(root, content);
}
#endregion

#region 加载 保存 创建
/// <summary>
/// 加载xml文件
/// </summary>
private void LoadXml()
{
doc = new XmlDocument();
try
{
doc.Load(fileName);
}
catch
{
if (File.Exists(fileName))
_errorInfo = "文件加载失败";
else
_errorInfo = "文件路径不正确";
throw;
}
}
public void CreateSampleXmlFile(string root,string content)
{
doc = new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "gb2312", null));//设置xml的版本,格式信息
XmlNode rn = doc.CreateElement("", root, "");
rn.InnerText = content;
doc.AppendChild(rn);//创建根元素
}
public void SaveXml()
{
if (doc != null)
{
try
{
doc.Save(fileName);
}
catch
{
_errorInfo = "文件保存失败,可能是路径不对或者是没有修改权限";
throw;
}
finally
{
doc = null;
}
}
else { _errorInfo = "文件保存失败,可能是文件路径不对或者是对文件没有权限"; }
}
#endregion

#region 查找节点 根节点 父节 子节点
public Object GetRootElement()//XmlElement
{
return doc.DocumentElement;
}
public Object GetXmlNode(string XPath)//XmlNode
{
node = doc.SelectSingleNode(XPath);
if (node != null)
return node;
else
{
_errorInfo = "未找到"+XPath+"的节点";
return null;
}
}
public Object GetNodeChildNode(string XPath)//XmlNodeList
{
GetXmlNode(XPath);
if (node != null)
return node.ChildNodes;
else
return null;
}
public Object[] GetNodeChildNodes(string xPath)
{
GetXmlNode(xPath);
Object[] arrNds = null;
if (node != null)
{
XmlNodeList ndlts = node.ChildNodes;
int count = ndlts.Count;
if (count > 0)
{
arrNds = new object[count];
for (int i = 0; i < count; i++)
{
arrNds[i] = ndlts[i];
}
}
}
return arrNds;
}
public int GetNodeListsMaxValue(string xPath, bool isAttr,string attr)
{
int id = 0, lid=0 ;
XmlNodeList nlist=(XmlNodeList) GetNodeChildNode(xPath);
try
{
for (int i = 0; i < nlist.Count; i++)
{
if (isAttr)
lid = Int32.Parse(nlist[i].Attributes[attr].Value);
else
lid = Int32.Parse(nlist[i].InnerText);
if (lid > id)
id = lid;
}
}
catch { _errorInfo = "查找失败,类型转换失败,请属性或者是innerText的数据类型"; throw; }
return id;
}
public Object GetParentNode(Object node)//XmlNode
{
XmlNode xnode = (XmlNode)node;
XmlNode pnode = null;
try
{
pnode = xnode.ParentNode;
}
catch
{
_errorInfo = "查找父节点失败,请检查" + xnode.Name + "节点";
throw;
}
return pnode;
}
#endregion
...全文
133 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
CutBug 2009-02-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zlb789 的回复:]
引用 10 楼 CutBug 的回复:
public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
key/value Dictionary <string,string>


原来也想过用 Dictionary <string,string>
但是不知道在这里具体有什么好处
[/Quote]
代码代码可读性,比如有20对key/value,2个数组,一个一个对那是很痛苦的一件事
royrandy 2009-02-27
  • 打赏
  • 举报
回复
Mask
zlb789 2009-02-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 CutBug 的回复:]
public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
key/value Dictionary <string,string>
[/Quote]

原来也想过用 Dictionary <string,string>
但是不知道在这里具体有什么好处
deepinnet 2009-02-27
  • 打赏
  • 举报
回复
帮顶
real_name 2009-02-26
  • 打赏
  • 举报
回复
顶一下
ffyyn 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 camelials 的回复:]
不建议用XmlNodeList去查大的xml,建议使用流模式去读写xml.
[/Quote]

stream好象也是读区整个文件进行操作啊.. 你是不是想说xpath?
CutBug 2009-02-26
  • 打赏
  • 举报
回复
public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
key/value Dictionary<string,string>
wen_eric 2009-02-26
  • 打赏
  • 举报
回复
顶一下楼主
cppfaq 2009-02-26
  • 打赏
  • 举报
回复
个人意见,楼主的异常处理做的有点粗糙
wuyq11 2009-02-26
  • 打赏
  • 举报
回复
结构还要优化些,通过泛型操作节点内容
http://www.cnblogs.com/aidydream/articles/1279451.html
zlb789 2009-02-26
  • 打赏
  • 举报
回复
我看BlogEngine.NET 就是才用流的方式
不知道这样好处在那

另外 操作xml文件 一般都要对文件或者文件夹有相应的权限 , 用流方式在这方面是否需要

我记得修改webconfig 有的方法不需要文件权限 不过方式不一样 ,好象通过的另外的一个命名空间的类实现的

BossFriday 2009-02-26
  • 打赏
  • 举报
回复
不建议用XmlNodeList去查大的xml,建议使用流模式去读写xml.
dd__dd 2009-02-26
  • 打赏
  • 举报
回复
up
zlb789 2009-02-26
  • 打赏
  • 举报
回复
刚刚写好 ,
帮忙看看那些地方需要改进
hhxxcj 2009-02-26
  • 打赏
  • 举报
回复
呵呵,还行,帮你顶
zlb789 2009-02-26
  • 打赏
  • 举报
回复

#region 获取值
public string GetXmlNodeValue(string XPath)
{
GetXmlNode(XPath);
if (node != null)
return node.InnerText;
else
return null;
}
public string GetXmlNodeValue(Object xNode)
{
XmlNode nd = (XmlNode)xNode;
return node.InnerText;
}
public string GetXmlNodeAtrribut(string XPath, string attr)
{
GetXmlNode(XPath);
string attribute = string.Empty;
if (node != null)
{
try
{
attribute = node.Attributes[attr].Value;
}
catch
{
_errorInfo = "该节点不存在" + attr + "属性";
throw;
}
}
return attribute;
}
public string GetXmlNodeAtrribut(Object xNode, string attr)
{
string value = string.Empty;
XmlNode nd = (XmlNode)xNode;
try
{
value = nd.Attributes[attr].Value;
}
catch { _errorInfo = "该节点不存在" + attr + "属性"; throw; }
return value;
}
#endregion
#region 更新 XmlNode
public bool UpdateXmlNode(string XPath, string value)
{
GetXmlNode(XPath);
if (node != null)
{
node.InnerText = value;//执行后这个函数后记得调用保存函数
return true;
}
else
return false;
}
public bool UpdateXmlNode(string XPath, string content, string[] attri, string[] values)
{
GetXmlNode(XPath);
if (node != null)
{
node.InnerText = content;
int length = attri.Length;
for (int i = 0; i < length; i++)
{
node.Attributes[attri[i]].InnerText = values[i];
}
return true;
}
else
return false;
}
public bool UpdateXmlNodeAttribute(string XPath, string attr,string content)
{
GetXmlNode(XPath);
if (node != null)
{
node.Attributes[attr].InnerText = content;//执行后这个函数后记得调用保存函数
return true;
}
else
return false;
}
public void ReplaceNode(Object node, Object del_Node, Object new_Node)
{
XmlNode pnode = (XmlNode)node;
XmlNode delNode = (XmlNode)del_Node;
XmlNode newNode = (XmlNode)new_Node;
pnode.ReplaceChild(newNode, delNode);
}
#endregion

#region 创建节点
public Object CreateNewXmlNode(string xnode, string innerText)//XmlNode
{
XmlNode node = doc.CreateElement(xnode);
node.InnerText = innerText;
return node;
}
public Object CreateAttribute(string attr, string value)//XmlAttribute
{
XmlAttribute att = doc.CreateAttribute(attr);
att.Value = value;
return att;
}
#endregion

#region 插入
public void AppendChild(Object node, Object root_Node)
{
XmlNode xnode = (XmlNode)node;
XmlNode rootNode = (XmlNode)root_Node;
try
{
rootNode.AppendChild(xnode);
}
catch
{
_errorInfo = "给父节点中添加子节点失败`";
throw;
}
}
public void AppendChildAttr(Object Attr, Object root_Node)
{
XmlAttribute xAttr = (XmlAttribute)Attr;
XmlNode rootNode = (XmlNode)root_Node;
try
{
rootNode.Attributes.Append(xAttr);
}
catch
{
_errorInfo = "给节点添加属性时出错";
throw;
}
}
public void AppendChild(string pnode, string name, string content)
{
XmlNode pNode, node;
pNode = (XmlNode)GetXmlNode(pnode);
node = (XmlNode)CreateNewXmlNode(name, content);
AppendChild(node, pNode);
}
public void AppendAttribute(string pnode, string att, string value)
{
node = (XmlNode)GetXmlNode(pnode);
XmlAttribute attri = (XmlAttribute)CreateAttribute(att, value);
AppendChild(attri, node);
}
public void AppendAttribute(string pnode, string[] att, string[] values)
{
int length=att.Length;
if (length!= values.Length)
return;
node = (XmlNode)GetXmlNode(pnode);
XmlAttribute attri;
for (int i = 0; i < length; i++)
{
attri = (XmlAttribute)CreateAttribute(att[i], values[i]);
AppendChild(attri, node);
}
}
#endregion


#region 删除
public bool DelNode(Object p_node, Object _node)
{
XmlNode pnode = (XmlNode)p_node;
XmlNode node = (XmlNode)_node;
try
{
pnode.RemoveChild(node);
return true;
}
catch
{
_errorInfo = "从节点"+pnode .Name+"删除"+node .Name +"失败";
throw;
}
}
public bool DelNode(string Node)
{
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
XmlNode pnode,node;
try
{
pnode = (XmlNode)GetXmlNode(mainNode);
node = (XmlNode)GetXmlNode(Node);
}
catch {
throw ;
}
return DelNode(pnode, node);
}
#endregion

#region 返回一个 DateView

public DataView GetDate(string XPath)
{
DataSet ds = new DataSet();
GetXmlNode(XPath);
if (node != null)
{
StringReader read = new StringReader(node.OuterXml);
ds.ReadXml(read);
if (ds.Tables.Count > 0)
return ds.Tables[0].DefaultView;
else
return null;
}
else
return null;
}
#endregion

}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧