xml操作(三)添加xml节点的子节点(针对上节的改进)

一  改进的地方:

 增加了两个函数 AddNodeToXml 和AddChildNode ,增加的代码如下图:


二  显示效果



三 代码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CsharpConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            HOSPITAL hosp = new HOSPITAL() { HospId = 1, HospName ="中医院"};
            hosp.listDept = new List<DEPT>();
            hosp.listDept.Add(new DEPT() { DeptId = 101 , DeptName ="外科" ,DoctorNum = 16});
            hosp.listDept.Add(new DEPT() { DeptId = 201, DeptName = "眼科", DoctorNum = 5 });
            hosp.listDept.Add(new DEPT() { DeptId = 202, DeptName = "肾内科", DoctorNum = 11 });

            XmlDocument xmlDoc = new XmlDocument();//创建xml文档对象
            XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);//创建xml声明
            xmlDoc.AppendChild(xmlDec);//将xml声明添加到xml文档对象

            XmlElement xmlRoot = AddNodeToXml(xmlDoc,"HOSPITAL",""); //创建根节点

            XmlElement xmlElem = AddChildNode(xmlRoot, "HOSPNAME", hosp.HospName); //创建医院名称节点

            XmlElement xmlElemDepts = AddChildNode(xmlRoot, "DEPTS", "");  //创建科室节点
           
            

         
            for (i = 0; i < hosp.listDept.Count; i++)
            {
             
               XmlElement xmlElemDeptChild =  AddChildNode(xmlElemDepts, "DEPTS", "");
               AddChildNode(xmlElemDeptChild, "DEPTNAME", hosp.listDept[i].DeptName);
               AddChildNode(xmlElemDeptChild, "DEPTID", hosp.listDept[i].DeptId.ToString());
               System.Console.WriteLine(hosp.listDept[i].DeptName);
            }
            xmlDoc.Save("gj.xml");
            System.Console.ReadLine();
        }

        //向xml文档中添加节点
        static XmlElement  AddNodeToXml(XmlDocument xmlDoc, string elementName, string innerText)
        {
           XmlElement xmlElem = xmlDoc.CreateElement(elementName);
           if (!string.IsNullOrEmpty(innerText))
           {
               xmlElem.InnerText = innerText;
           }
           xmlDoc.AppendChild(xmlElem);
           return xmlElem;
        }

        //向xml中的指定节点中添加子节点
        static XmlElement AddChildNode(XmlElement parentNode , string elementName , string innerText)
        {
           XmlElement childNode =  parentNode.OwnerDocument.CreateElement(elementName);
           if (!string.IsNullOrEmpty(innerText))
           {
               childNode.InnerText = innerText;
           }
           parentNode.AppendChild(childNode);
           return childNode;
        }
    }

    class HOSPITAL
    {
        public int HospId{get;set;}//医院编号
        public string HospName{get;set;}//医院名字
        public List<DEPT> listDept;//科室信息
        public string Desc{get;set;}//医院描述信息
    }
    class DEPT
    {
        public int DeptId { get; set; }//科室id
        public string DeptName { get; set; }//科室名字
        public string Desc { get; set; }//描述信息
        public int DoctorNum { get; set; }//医生人数
    }


}

四  为什么这样改?

用函数的封装性,减少重复代码,主要是对xml类提供的两个函数 CreateElement 和 AppendChild 的封装。 


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值