您的位置:首页 > 编程语言 > C#

c# 写的一个xml配置工具,自我感觉很强大(wpf)

2016-06-30 08:53 363 查看


c# 写的一个xml配置工具,自我感觉很强大(wpf)

使用递归获取xml所有节点,并把所有节点以树的形式展示,
//对应于节点来说一般都有注释,所以默认的xml节点上面的那个注释就和这个节点绑定到一起了,也可以没有
支持每个节点的增删改查,包括同时操作这个节点的注释,
不多说,奉上方法(其实只是供参考的,想了解xml的增删改查看这里)
 
这个是根据xml的路径获取xml文档,并且实例第一个根节点

#region 获取xml节点
/// <summary>
/// 获取xml节点
/// </summary>
/// <param name="url"></param>
public void GetXml(string url)
{
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.Load(url);
}
catch (Exception e)
{
System.Windows.MessageBox.Show(e.Message);
return;
}

newurl = url;
//得到顶层节点列表
XmlNodeList topM = xmldoc.DocumentElement.ChildNodes;

//xml不为空
if (topM == null)
{
return;
}
XmlTv.Items.Clear();
//添加主节点
XmlNodeModel _XmlNodeModel = new XmlNodeModel();
_XmlNodeModel.NodeName = topM.Item(0).ParentNode.Name;
_XmlNodeModel.XmlNode = topM.Item(0).ParentNode;
TreeViewItem RootTvItem = CreateTvNode(_XmlNodeModel);
RootTvItem.IsExpanded = true;
XmlTv.Items.Add(RootTvItem);
//递归获取节点
GetNode(topM.Item(0), Guid.Empty, RootTvItem.Items);
}
#endregion


 
接下来就是关键的递归方法了,同时添加树,如果注意看会发现里面有一些费代码,嘿嘿

#region 递归获取节点
/// <summary>
/// 递归获取节点
/// </summary>
/// <param name="_CommentNode">节点</param>
/// <param name="_PGuid">父guid</param>
/// <param name="_TvItem">树节点</param>
private void GetNode(XmlNode _CommentNode, Guid _PGuid, ItemCollection _TvItem)
{
XmlNodeModel xmlModle = new XmlNodeModel();
XmlNode _Node = null;
if (_CommentNode.NodeType == XmlNodeType.Comment)
{
//注释
xmlModle.NodeComment = _CommentNode.Value;
xmlModle.CXmlNode = _CommentNode;
//获取注释下面的节点
_Node = _CommentNode.NextSibling;
if (_Node.NodeType == XmlNodeType.Comment)
{
XmlTv.Items.Clear();//清空xml树
System.Windows.Forms.MessageBox.Show("程序不允许两个或更多注释连续存在,请修改注释节点:" + _Node.Value);
return;
}
}
else
{
_Node = _CommentNode;

}
if (_Node == null)
{
return;
}
xmlModle.NodeGuid = Guid.NewGuid();
xmlModle.ParentNodeGuid = _PGuid;
xmlModle.NodeName = _Node.Name;
xmlModle.NodeItemList = new List<XmlItemModel>();
//节点项
if (_Node.Attributes != null)
{
foreach (XmlNode mainNode in _Node.Attributes)
{
XmlItemModel xmlItem = new XmlItemModel();
//key="123"
xmlItem.Key = mainNode.Name;//key
xmlItem.Value = mainNode.Value;//123
xmlModle.NodeItemList.Add(xmlItem);
}
}

xmlModle.IsChildNode = _Node.HasChildNodes;
xmlModle.XmlNode = _Node;
//添加到xml列表
CaCheCenter.XmlNodeModelList.Add(xmlModle);
//添加到树节点
TreeViewItem _TreeViewItem = CreateTvNode(xmlModle);
_TvItem.Add(_TreeViewItem);
//如果有子节点
if (_Node.HasChildNodes)
{
GetNode(_Node.ChildNodes.Item(0), xmlModle.NodeGuid, _TreeViewItem.Items);
}
//如果下个节点是空
if (_Node.NextSibling == null)
{
//隐藏选择按钮
SelectXmlBut.Visibility = Visibility.Collapsed;
return;
}
GetNode(_Node.NextSibling, _PGuid, _TvItem);
}
#endregion


 
以上就把整棵树建好了,缺少的方法自己补全~!
添加方法(这个是添加节点的方法,不是节点里的属性,添加删除属性往下看)

#region 添加节点
/// <summary>
/// 添加节点
/// </summary>
/// <param name="nodeName">节点名</param>
/// <param name="nodeItemList">属性列表,逗号分开</param>
private void AddNode(string nodeComment, string nodeName, List<string> nodeItemList)
{
//获取当前选择的节点
XmlNode thisNode = (((XmlTv.SelectedItem as TreeViewItem).DataContext) as XmlNodeModel).XmlNode;
if (thisNode == null)
{
return;
}
if (Convert.ToBoolean(AddNodeBottonRb.IsChecked))//节点末尾
{
if (nodeComment != null && nodeComment != "")
{
//添加注释
XmlComment comm = thisNode.OwnerDocument.CreateComment(nodeComment);//注释
thisNode.AppendChild(comm);
}

//添加节点
XmlElement elem = thisNode.OwnerDocument.CreateElement(nodeName);//节点名
//添加到选择的节点下面
thisNode.AppendChild(elem);

if (nodeItemList != null)
{
foreach (string nodeitem in nodeItemList)
{
XmlAttribute xa = thisNode.OwnerDocument.CreateAttribute(nodeitem.Split(',')[0]);//节点属性key
xa.Value = nodeitem.Split(',')[1];//节点属性value
elem.SetAttributeNode(xa);
}
}
}
else//节点头部
{
//添加节点
XmlElement elem = thisNode.OwnerDocument.CreateElement(nodeName);//节点名
//添加到选择的节点下面
thisNode.PrependChild(elem);

if (nodeItemList != null)
{
foreach (string nodeitem in nodeItemList)
{
XmlAttribute xa = thisNode.OwnerDocument.CreateAttribute(nodeitem.Split(',')[0]);//节点属性key
xa.Value = nodeitem.Split(',')[1];//节点属性value
elem.SetAttributeNode(xa);
}
}
if (nodeComment != null && nodeComment != "")
{
//添加注释
XmlComment comm = thisNode.OwnerDocument.CreateComment(nodeComment);//注释
thisNode.PrependChild(comm);
}
}

thisNode.OwnerDocument.Save(newurl);
AgainLoad();
System.Windows.MessageBox.Show("添加成功!");
}
#endregion


 
修改方法

#region 修改节点
/// <summary>
/// 修改节点
/// </summary>
/// <param name="_XmlNode"></param>
private void UpdateNode()
{
if (XmlTv.SelectedItem == null)
{
System.Windows.MessageBox.Show("请选择要修改的节点!");
return;
}
//获取当前选择的节点
XmlNode thisNode = (((XmlTv.SelectedItem as TreeViewItem).DataContext) as XmlNodeModel).XmlNode;
XmlNode thisCNode = (((XmlTv.SelectedItem as TreeViewItem).DataContext) as XmlNodeModel).CXmlNode;
if (thisNode == null)
{
return;
}
thisCNode.Value = CommentTb.Text;
thisNode.Attributes.RemoveAll();
foreach (XmlItemModel item in (ItemListBox.ItemsSource as List<XmlItemModel>))
{
if (item.Key != null && item.Key != "")
{
XmlAttribute xa = thisNode.OwnerDocument.CreateAttribute(item.Key);//节点属性key
xa.Value = item.Value;//节点属性value
(thisNode as XmlElement).SetAttributeNode(xa);
}
}

thisNode.OwnerDocument.Save(newurl);
AgainLoad();
System.Windows.MessageBox.Show("修改成功!");
}
#endregion


 
删除方法

#region 删除节点
/// <summary>
/// 删除节点
/// </summary>
/// <param name="_XmlNode"></param>
private void DeleteNode()
{
if (XmlTv.SelectedItem == null)
{
System.Windows.MessageBox.Show("请选择要删除的父节点");
return;
}
//获取当前选择的节点
XmlNode thisNode = (((XmlTv.SelectedItem as TreeViewItem).DataContext) as XmlNodeModel).XmlNode;
XmlNode thisCNode = (((XmlTv.SelectedItem as TreeViewItem).DataContext) as XmlNodeModel).CXmlNode;

if (MessageBoxResult.OK != System.Windows.MessageBox.Show("确定要删除" + thisNode.Name + "(" + thisCNode.Value + ")节点么?", "", MessageBoxButton.OKCancel))
{
return;
}

thisNode.ParentNode.RemoveChild(thisNode);
if (thisCNode != null)
{
thisCNode.ParentNode.RemoveChild(thisCNode);
}
thisNode.OwnerDocument.Save(newurl);
AgainLoad();
System.Windows.MessageBox.Show("删除成功!");
}
#endregion


 
属性操作的(修改是和节点一起的)

#region 添加属性
/// <summary>
/// 添加属性
/// </summary>
private void AddNodeItem(bool IsUpdata)
{
if (IsUpdata)
{
//修改里
List<XmlItemModel> itemlist = ItemListBox.ItemsSource as List<XmlItemModel>;
if (itemlist == null)
{
itemlist = new List<XmlItemModel>();
}
XmlItemModel item = new XmlItemModel();
item.Key = "";
item.Value = "";
itemlist.Add(item);
ItemListBox.ItemsSource = null;
ItemListBox.ItemsSource = itemlist;
return;
}
//添加里
List<XmlItemModel> additemlist = Add_ItemListBox.ItemsSource as List<XmlItemModel>;
if (additemlist == null)
{
additemlist = new List<XmlItemModel>();
}
XmlItemModel additem = new XmlItemModel();
additem.Key = "";
additem.Value = "";
additemlist.Add(additem);
Add_ItemListBox.ItemsSource = null;
Add_ItemListBox.ItemsSource = additemlist;
}
#endregion


 
 

#region 删除属性
/// <summary>
/// 删除属性
/// </summary>
private void DelNodeItem(bool IsUpdate, XmlItemModel DelItem)
{
//修改里
if (IsUpdate)
{
List<XmlItemModel> list = ItemListBox.ItemsSource as List<XmlItemModel>;
if (list != null)
{
list.Remove(DelItem);
}
ItemListBox.ItemsSource = null;
ItemListBox.ItemsSource = list;
return;
}
//添加里
List<XmlItemModel> addlist = Add_ItemListBox.ItemsSource as List<XmlItemModel>;
if (addlist != null)
{
addlist.Remove(DelItem);
}
Add_ItemListBox.ItemsSource = null;
Add_ItemListBox.ItemsSource = addlist;
}
#endregion


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: