xml 操作2

本文介绍了一个使用C#实现的XML文档管理类,包括读取、写入、修改、插入和删除XML节点的方法,提供了丰富的功能来处理XML数据。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
//两个构造函数中,一个是XML文件路径,一个文件路径加上节点路径,在重载的函数中有的需要设置节点路径
//方法为:xmlManager.NodePath = "\\子节点\子节点……"
//所有异常全部抛出,需要在程序捕捉
namespace Aidy.XmlManager
{
    
public class XmlManager
    {
        
#region 变量的定义

        
private XmlDocument _document = new XmlDocument();
        
private string _xmlPath;//文件路径
        private string _nodePath;//节点路径        
        /// <summary>
        
/// 操作的XML文档路径=文件路径+文件全名
        
/// </summary>
        public string XmlPath
        {
            
get { return _xmlPath; }
            
set { _xmlPath = value; }
        }
        
/// <summary>
        
/// XML文档中的节点路径"//根节点/子节点/子节点……"
        
/// </summary>
        public string NodePath
        {
            
get { return _nodePath; }
            
set { _nodePath = value; }
        }
        
#endregion

        
#region 构造函数        
        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="xmlPath">XML文档路径+xml文件全名</param>
        public XmlManager(string xmlPath,string nodePath)
        {
            
this._xmlPath = xmlPath;
            
this._nodePath = nodePath;
            
this._document.Load(xmlPath);
        }
        
/// <summary>
        
/// 构造函数
        
/// </summary>
        
/// <param name="xmlName">默认程序运行路径和XML文件名</param>
        public XmlManager(string xmlPath)
        {
            
this._xmlPath = xmlPath;
            
this._document.Load(xmlPath);
        }
        
#endregion

        
#region 各种操作方法
        
/// <summary>
        
/// 读出指定路径XML文档的全部内容
        
/// </summary>
        
/// <returns>XML文档的全部内容</returns>
        public string Out()
        {
            
return this._document.OuterXml;
        }
        
#region 获取节点
        
/// <summary>
        
/// 获取XML中节点的内容
        
/// </summary>
        
/// <param name="nodeName">节点名称</param>
        
/// <returns>List(string)集合</returns>
        public List<string> GetNode(string nodeName)
        {
            
try
            {
                List
<string> list = new List<string>();
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(nodeName);
                
//string[] li = new string[nodeList.Count];
                foreach (XmlNode node in nodeList)
                {
                    list.Add(node.InnerText);
                }
                
return list;
            }
            
catch (Exception e)
            {
                
throw(new Exception(e.Message));
                
            }
        }
        
/// <summary>
        
/// 获取XML中节点的内容
        
/// </summary>
        
/// <param name="i">第i段数据</param>
        
/// <param name="j">第j个属性</param>
        
/// <param name="path">用户指定节点路径</param>
        
/// <returns></returns>
        public string GetNode(int i,int j,string path)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(path);
                
                
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 获取XML中节点的内容
        
/// </summary>
        
/// <param name="i">第i段数据</param>
        
/// <param name="j">第j个属性</param>
        
/// <returns></returns>
        public string GetNode(int i, int j)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(_nodePath);

                
return nodeList.Item(i - 1).ChildNodes.Item(j - 1).InnerText;
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 获取XML中节点的内容
        
/// </summary>
        
/// <param name="i">第i段数据</param>
        
/// <param name="nodeName">节点名</param>
        
/// <param name="nodePath">用户指定路径</param>
        
/// <returns></returns>
        public string GetNode(int i,string nodePath, string nodeName)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(nodePath);
                
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
                {
                    
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
                        
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText;

                }
                
return "nofind";
            }
            
catch(Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 获取XML中节点的内容
        
/// </summary>
        
/// <param name="i">第i段数据</param>
        
/// <param name="nodeName">节点名</param>
        
/// <returns></returns>
        public string GetNode(int i,string nodeName)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(_nodePath);
                
for (int j = 0; j <= nodeList.Item(i - 1).ChildNodes.Count; j++)
                {
                    
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
                        
return nodeList.Item(i - 1).ChildNodes.Item(j).InnerText;

                }
                
return "nofind";
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
        
#endregion

        
#region 获取节点数
        
/// <summary>
        
/// 返回符合指定名称的节点数
        
/// </summary>
        
/// <param name="nodeName">节点名</param>
        
/// <returns>节点数</returns>
        public int Count(string nodeName)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(nodeName);
                
return nodeList.Count;
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 使用设置的节点路径返回符合指定名称的节点数
        
/// </summary>       
        
/// <returns>节点数</returns>
        public int Count()
        {
            
                
try
                {

                    XmlNodeList nodeList 
= this._document.SelectNodes(_nodePath);
                    
return nodeList.Count;
                }
                
catch (Exception e)
                {
                    
throw (new Exception(e.Message));
                }
            
        }
        
/// <summary>
        
/// 返回指符合指定名称的节点集中第i个集合的子节点数
        
/// </summary>
        
/// <param name="nodePath"></param>
        
/// <returns></returns>
        public int CountChilds(int i,string nodeName)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(nodeName);
                
if (nodeList.Count > 0)
                {
                    
return nodeList.Item(i-1).ChildNodes.Count;
                }
                
else
                {
                    
return 0;
                }
            }
            
catch(Exception e)
            {
                
throw(new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 返回指符合指定名称的节点的子节点数
        
/// </summary>
        
/// <param name="nodePath"></param>
        
/// <returns></returns>
        public int CountChilds(string nodeName)
        {
            
try
            {
                
int counts = 0;
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(nodeName);
                
if (nodeList.Count > 0)
                {
                    
for (int i = 0; i < nodeList.Count; i++)
                    {
                        counts 
+= nodeList.Item(i).ChildNodes.Count;
                    }
                    
return counts;
                }
                
else
                {
                    
return 0;
                }
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
#endregion

        
#region 修改节点值
        
/// <summary>
        
/// 修改指定节点的值
        
/// </summary>
        
/// <param name="nodeName">节点名称</param>
        
/// <param name="newValue">新值</param>
        
/// <returns>返回更改的节点数</returns>
        public int SetNode(string nodeName,string newValue)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(nodeName);
                
foreach(XmlNode node in nodeList)
                {
                    node.InnerText 
= newValue;
                }
                
this._document.Save(_xmlPath);
                
return nodeList.Count;
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
/// <summary>
        
/// 修改指定节点的值
        
/// </summary>
        
/// <param name="i">第i段数据</param>
        
/// <param name="nodePath">节点路径</param>
        
/// <param name="nodeName">节点名称</param>
        
/// <param name="newValue">新值</param>
        public void SetNode(int i,string nodePath,string nodeName,string newValue)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(nodePath);
                
if (nodeList.Count > 0 && nodeList.Count <= i)
                {
                    
for (int j = 0; j < nodeList.Item(i - 1).ChildNodes.Count; j++)
                    {
                        
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
                        {
                            nodeList.Item(i 
- 1).ChildNodes.Item(j).InnerText = newValue;
                            
this._document.Save(_xmlPath);

                        }
                    }
                }
                
else
                {
                    
throw (new Exception("并无此子集"));
                }
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
        
/// <summary>
        
/// 修改指定节点的值,使用设置的节点路径
        
/// <param name="i">第i段数据</param>
        
/// <param name="nodeName">节点名称</param>
        
/// <param name="newValue">新值</param>
        public void SetNode(int i, string nodeName, string newValue)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.SelectNodes(_nodePath);
                
if (nodeList.Count > 0 && nodeList.Count <= i)
                {
                    
for (int j = 0; j < nodeList.Item(i - 1).ChildNodes.Count; j++)
                    {
                        
if (nodeList.Item(i - 1).ChildNodes.Item(j).Name == nodeName)
                        {
                            nodeList.Item(i 
- 1).ChildNodes.Item(j).InnerText = newValue;
                            
this._document.Save(_xmlPath);
                        }
                    }
                }
            }
            
catch (Exception e)
            {
                
throw (new Exception(e.Message));
            }
        }
        
#endregion

        
#region 插入节点
        
/// <summary>
        
/// 为指定的节点(集插)入一个子节点
        
/// </summary>
        
/// <param name="parentName">父节点</param>
        
/// <param name="nodeName">子节点名</param>
        
/// <param name="nodeVale">子节点值</param>
        public void InsetNode(string parentName,string nodeName,string nodeVale)
        {
            XmlNodeList nodeList 
= this._document.GetElementsByTagName(parentName);       
            
for (int i = 0; i < nodeList.Count; i++)
            {
                XmlElement newElement 
= _document.CreateElement(nodeName);
                
//newElement.SetAttribute(nodeName,nodeVale);//会插入如<id id = "0012"/>的节点
                nodeList.Item(i).AppendChild(newElement);//<id>002</id>
                newElement.InnerText = nodeVale;
            }
            
this._document.Save(this._xmlPath);
 
        }
        
/// <summary>
        
/// 为指定的节点(集)插入一个子节点
        
/// </summary>
        
/// <param name="parentName">父节点</param>
        
/// <param name="nodeName">子节点名</param>
        
/// <param name="nodeVale">子节点值</param>
        public void InsetNode(int i,string parentName, string nodeName, string nodeValue)
        {
            
try
            {
                XmlNodeList nodeList 
= this._document.GetElementsByTagName(parentName);
                XmlElement newElement 
= _document.CreateElement(nodeName);
                
//newElement.SetAttribute(nodeName, nodeValue);
                if (nodeList.Count > 0 && nodeList.Count <= i)
                {
                    nodeList.Item(i 
- 1).AppendChild(newElement);
                    newElement.InnerText 
= nodeValue;

                    
this._document.Save(this._xmlPath);
                }
                
else
                {
                    
throw(new Exception("无此节点"));
                }
            }
            
catch(Exception e)
            {
                
throw(new Exception(e.Message));
            }
            
        }
        
/// <summary>
        
/// 插入根节点(第二级根节点)
        
/// </summary>
        
/// <param name="rootName">节点名</param>
        
/// <param name="nodeName">子节点名</param>
        
/// <param name="nodeValue">子节点值</param>
        public void InsertRootNode(string rootName,string[] nodeName,string[] nodeValue)
        {
            XmlElement root 
= _document.DocumentElement;
            XmlElement newRoot 
= _document.CreateElement(rootName);
            root.AppendChild(newRoot);
            
for (int i = 0; i < nodeName.Length; i++)
            {
                XmlElement newChild 
= _document.CreateElement(nodeName[i]);
                newRoot.AppendChild(newChild);
                newChild.InnerText 
= nodeValue[i];
            }
            
this._document.Save(this._xmlPath);
        }

        
        
//public string geT(string name)
        
//{
        
//    XmlNodeList n = _document.GetElementsByTagName(name);
        
//    XmlElement nn = _document.
        
//    return nn[0].Attributes.ToString();
        
//}
        #endregion

        
public void DeleteNote(string parentName, string noteName)
        {
            XmlNodeList nodeList 
= _document.GetElementsByTagName(parentName);
            
//for (int i = 0; i < nodeList.Count; i++)
            
//    if (nodeList.Item(i).ParentNode.Name == parentName)
            
//        nodeList.Item(i).ParentNode.RemoveChild(nodeList.Item(i));
            
//this._document.Save(this._xmlPath);            
            
//for (int i = 0; i < nodeList.Count; i++)
            
//    for (int j = 0; j < nodeList.Item(i).ChildNodes.Count; j++)
            
//        if (nodeList.Item(i).ChildNodes.Item(j).Name == noteName)
            
//            nodeList.Item(i).RemoveChild(nodeList.Item(i).ChildNodes.Item(j));
            
//this._document.Save(this._xmlPath);
            foreach (XmlNode node in nodeList)
            {
                
foreach (XmlNode nodechild in node.ChildNodes)
                    
if (nodechild.Name == noteName)
                    {
                        node.RemoveChild(nodechild);
                    }
            }
            
this._document.Save(this._xmlPath);
        }
        
public void DeleteNote(string parmentName)
        {
            XmlNodeList nodeList 
= this._document.GetElementsByTagName(parmentName);
            
foreach (XmlNode node in nodeList)
                node.RemoveAll();
            
this._document.Save(_xmlPath);
        }
        
public void DeleteAll()
        {
            XmlElement element 
= this._document.DocumentElement;
            element.RemoveAll();
            
this._document.Save(this._xmlPath);
        }
        
public Boolean Save(string xmlName, string rootElement)
        {
            
try
            {
                
string savePath = System.IO.Directory.GetCurrentDirectory()+"\\"+xmlName;
                XmlDocument document 
= new XmlDocument();
                XmlElement element 
= document.CreateElement(rootElement);
                document.AppendChild(element);
                document.Save(savePath);
                
return true;
            }
            
catch (XmlException xe)
            {
                
throw (new Exception(xe.Message));
            }
        }
        
#endregion
    }
}

转载于:https://www.cnblogs.com/zhdonghu/archive/2011/01/17/1937736.html

内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值