C#读出和写入XML文件

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

namespace PrintXML
{
    class Program
    {
        static void Main(string[] args)
        {
            //读取标准字段的xml中的英文名和中文名;
            XmlDocument xmlDoc = new XmlDocument();
            string xmlpath = "E:/xxx1.xml";
            xmlDoc.Load(xmlpath);
            XmlNode root = xmlDoc.DocumentElement;
            XmlNodeList xnlist = root.ChildNodes;

            Dictionary<string, string> myDirectory = new Dictionary<string, string>();
            foreach (XmlNode node in xnlist)
            {
                if (node.Attributes["name"] != null)
                {
                    string str1 = node.Attributes["name"].Value;
                    string str2 = node.Attributes["chineseName"].Value;
                    myDirectory[str1] = str2;
                }
            }

            //读取异构组件的name;
            XmlDocument xml = new XmlDocument();
            string path = "F:/任务/xxx2.xml";
            xml.Load(path);
            XmlNode root2 = xml.DocumentElement;
            XmlNodeList xnlist2 = root2.ChildNodes;
            foreach (XmlNode itemsNode in xnlist2)
            {
                // 获取name属性
                XmlAttribute nameAttr = itemsNode.Attributes["name"];
                if (null == nameAttr)
                {
                    continue;
                }
                foreach (XmlElement itemNode in itemsNode.OfType<XmlElement>())
                {
                    XmlAttribute itemcName = itemNode.Attributes["cName"];
                    if(null != itemcName)
                    {
                        continue;
                    }
                    XmlAttribute itemName = itemNode.Attributes["name"];
                    if (null == itemName)
                    {
                        continue;
                    }
                    string ename = itemName.Value;

                    string cname;
                    if (myDirectory.TryGetValue(ename, out cname))
                    {
                        itemNode.SetAttribute("cName", cname);
                    }
                    else
                    {
                        Console.WriteLine("name={0}",ename);
                    }

                }

            }

            xml.Save(path + ".out1");
        }
    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值