解析世界各区域的中英文(将两个XML文件合并成一个json文件)

1:界面
在这里插入图片描述
2:执行代码

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace OperationRegionWindowsFormsApp6
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void uiButton1_Click(object sender, EventArgs ev)
        {
            try
            {
                // XML文件路径  
                string filePath = @"D:\C#Demo\WinformDemo\OperationRegionWindowsFormsApp6\OperationRegionWindowsFormsApp6\bin\Debug\resources.xml"; // 替换为你的文件路径  

                // 加载XML文件  
                XDocument xDoc = XDocument.Load(filePath);

                // 假设XML结构如上所示,并且我们想要获取阿富汗的信息  
                var afghanistanName = xDoc.Root.Elements("string")
                    .Where(e => (string)e.Attribute("name") == "country_afghanistan_name")
                    .Select(e => e.Value)
                    .FirstOrDefault();

                var afghanistanCode = xDoc.Root.Elements("string")
                    .Where(e => (string)e.Attribute("name") == "country_afghanistan_code")
                    .Select(e => e.Value)
                    .FirstOrDefault();

                var afghanistanNumber = xDoc.Root.Elements("string")
                    .Where(e => (string)e.Attribute("name") == "country_afghanistan_number")
                    .Select(e => e.Value)
                    .FirstOrDefault();

                // 输出结果  
                if (!string.IsNullOrEmpty(afghanistanName))
                {
                    Console.WriteLine($"Country Name: {afghanistanName}");
                }

                if (!string.IsNullOrEmpty(afghanistanCode))
                {
                    Console.WriteLine($"Country Code: {afghanistanCode}");
                }

                if (!string.IsNullOrEmpty(afghanistanNumber))
                {
                    Console.WriteLine($"Country Phone Number Prefix: {afghanistanNumber}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            try
            {
                // XML文件路径  
                string filePath = @"D:\C#Demo\WinformDemo\OperationRegionWindowsFormsApp6\OperationRegionWindowsFormsApp6\bin\Debug\resources.xml"; // 替换为你的文件路径  

                // 加载XML文件  
                XDocument xDoc = XDocument.Load(filePath);

                var afghanistanElements = xDoc.Root.Elements("string").ToList();
                if (afghanistanElements.Count % 3 == 0)
                {


                    string afghanistanName = afghanistanElements[0].Value;
                    string afghanistanCode = afghanistanElements[1].Value;
                    string afghanistanNumber = afghanistanElements[2].Value;
                    string my_show = string.Format("读取的国家名:{0},国家code:{1},国家number:{2}", afghanistanName, afghanistanCode, afghanistanNumber);
                    Console.WriteLine(my_show);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void uiButton3_Click(object sender, EventArgs e)
        {
            try
            {
                // 假设我们有一个整数数组,其长度是3的倍数  
                int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

                // 使用for循环,步长为3  
                for (int i = 0; i < numbers.Length; i += 3)
                {
                    // 读取当前三元组的三个元素  
                    int first = numbers[i];
                    int second = numbers[i + 1]; // 注意检查边界条件,但在这个例子中我们假设数组长度是3的倍数  
                    int third = numbers[i + 2];

                    // 输出这三个元素  
                    Console.WriteLine($"三元组: {first}, {second}, {third}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        public List<CountryClass> m_countryClasses = new List<CountryClass>();

        public List<CountryClass> m_new_countryClasses = new List<CountryClass>();
        private void uiButton4_Click(object sender, EventArgs ev)
        {
            try
            {
                // XML文件路径  
                string filePath = @"D:\C#Demo\WinformDemo\OperationRegionWindowsFormsApp6\OperationRegionWindowsFormsApp6\bin\Debug\resources.xml"; // 替换为你的文件路径  

                // 加载XML文件  
                XDocument xDoc = XDocument.Load(filePath);
                var afghanistanElements = xDoc.Root.Elements("string").ToList();
                if (afghanistanElements.Count % 3 == 0)
                {
                    // 使用for循环,步长为3  
                    for (int i = 0; i < afghanistanElements.Count; i += 3) 
                    {
                        string afghanistanName = afghanistanElements[i].Value;
                        string afghanistanCode = afghanistanElements[i+1].Value;
                        string afghanistanNumber = afghanistanElements[i+2].Value;
                        string my_show = string.Format("读取的国家名:{0},国家code:{1},国家number:{2}", afghanistanName, afghanistanCode, afghanistanNumber);
                        //Console.WriteLine(my_show);
                        CountryClass my_countryClass = new CountryClass();
                        my_countryClass.countryEnName = afghanistanName;
                        my_countryClass.countryZnCode = afghanistanCode;
                        my_countryClass.countryZnNumber = afghanistanNumber;
                        m_countryClasses.Add(my_countryClass);
                        Console.WriteLine(m_countryClasses.Count);
                    }
                }


                
                // XML文件路径  
                string znfilePath = @"D:\C#Demo\WinformDemo\OperationRegionWindowsFormsApp6\OperationRegionWindowsFormsApp6\bin\Debug\resourceszn.xml"; // 替换为你的文件路径  

                // 加载XML文件  
                XDocument xDoczn = XDocument.Load(znfilePath);
                var afghanistanElementszn = xDoczn.Root.Elements("string").ToList();
                foreach (CountryClass item in m_countryClasses)
                {
                    string my_country_en_name = item.countryEnName;
                    string my_country_en_name_low = my_country_en_name.ToLower();
                    string my_country_en_name_modifiy = my_country_en_name_low.Replace(" ", "_");
                    //country_afghanistan_name
                    string my_country_zn_node_name = string.Format("{0}_{1}_{2}", "country", my_country_en_name_modifiy, "name");
                    var afghanistanName = xDoczn.Root.Elements("string")
                    .Where(e => (string)e.Attribute("name") == my_country_zn_node_name)
                    .Select(e => e.Value)
                    .FirstOrDefault();

                    CountryClass my_new_countryClass = item;
                    my_new_countryClass.countryZnName = afghanistanName;

                    m_new_countryClasses.Add(my_new_countryClass);
                }


                Console.WriteLine(m_new_countryClasses.Count);

                string json = JsonConvert.SerializeObject(m_new_countryClasses, Newtonsoft.Json.Formatting.Indented);


                string my_json_path = @"D:\C#Demo\WinformDemo\OperationRegionWindowsFormsApp6\OperationRegionWindowsFormsApp6\bin\Debug\resources.json"; // 替换为你的文件路径  

                File.WriteAllText(my_json_path, json);

                Console.WriteLine("JSON file has been created.");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
    public class CountryClass 
    {
        /// <summary>
        /// 国家或区域英文名称
        /// </summary>
        public string countryEnName { get; set; }
        /// <summary>
        /// 国家或区域中文名称
        /// </summary>
        public string countryZnName { get; set; }
        /// <summary>
        /// 国家或区域code
        /// </summary>
        public string countryZnCode { get; set; }
        /// <summary>
        /// 国家或区域number
        /// </summary>
        public string countryZnNumber { get; set; }
    }
}

3:界面代码(界面中使用了sunny.UI组件,可以将按钮与输入框替换成系统自带的按钮和输入框类型)

namespace OperationRegionWindowsFormsApp6
{
    partial class Form4
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.uiButton1 = new Sunny.UI.UIButton();
            this.uiButton2 = new Sunny.UI.UIButton();
            this.uiButton3 = new Sunny.UI.UIButton();
            this.uiButton4 = new Sunny.UI.UIButton();
            this.uiTextBox1 = new Sunny.UI.UITextBox();
            this.SuspendLayout();
            // 
            // uiButton1
            // 
            this.uiButton1.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButton1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton1.Location = new System.Drawing.Point(25, 40);
            this.uiButton1.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButton1.Name = "uiButton1";
            this.uiButton1.Size = new System.Drawing.Size(100, 35);
            this.uiButton1.TabIndex = 0;
            this.uiButton1.Text = "读取xml";
            this.uiButton1.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton1.Click += new System.EventHandler(this.uiButton1_Click);
            // 
            // uiButton2
            // 
            this.uiButton2.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButton2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton2.Location = new System.Drawing.Point(170, 40);
            this.uiButton2.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButton2.Name = "uiButton2";
            this.uiButton2.Size = new System.Drawing.Size(100, 35);
            this.uiButton2.TabIndex = 1;
            this.uiButton2.Text = "循环读取xml";
            this.uiButton2.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton2.Click += new System.EventHandler(this.uiButton2_Click);
            // 
            // uiButton3
            // 
            this.uiButton3.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButton3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton3.Location = new System.Drawing.Point(331, 40);
            this.uiButton3.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButton3.Name = "uiButton3";
            this.uiButton3.Size = new System.Drawing.Size(182, 35);
            this.uiButton3.TabIndex = 2;
            this.uiButton3.Text = "循环一次读取三个数据";
            this.uiButton3.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton3.Click += new System.EventHandler(this.uiButton3_Click);
            // 
            // uiButton4
            // 
            this.uiButton4.Cursor = System.Windows.Forms.Cursors.Hand;
            this.uiButton4.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton4.Location = new System.Drawing.Point(25, 114);
            this.uiButton4.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiButton4.Name = "uiButton4";
            this.uiButton4.Size = new System.Drawing.Size(223, 35);
            this.uiButton4.TabIndex = 3;
            this.uiButton4.Text = "循环读取xml(每次三个数据)";
            this.uiButton4.TipsFont = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiButton4.Click += new System.EventHandler(this.uiButton4_Click);
            // 
            // uiTextBox1
            // 
            this.uiTextBox1.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.uiTextBox1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiTextBox1.Location = new System.Drawing.Point(331, 119);
            this.uiTextBox1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.uiTextBox1.MinimumSize = new System.Drawing.Size(1, 16);
            this.uiTextBox1.Name = "uiTextBox1";
            this.uiTextBox1.Padding = new System.Windows.Forms.Padding(5);
            this.uiTextBox1.ShowText = false;
            this.uiTextBox1.Size = new System.Drawing.Size(150, 29);
            this.uiTextBox1.TabIndex = 4;
            this.uiTextBox1.Text = "uiTextBox1";
            this.uiTextBox1.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.uiTextBox1.Watermark = "";
            // 
            // Form4
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.uiTextBox1);
            this.Controls.Add(this.uiButton4);
            this.Controls.Add(this.uiButton3);
            this.Controls.Add(this.uiButton2);
            this.Controls.Add(this.uiButton1);
            this.Name = "Form4";
            this.Text = "读取XML";
            this.ResumeLayout(false);

        }

        #endregion

        private Sunny.UI.UIButton uiButton1;
        private Sunny.UI.UIButton uiButton2;
        private Sunny.UI.UIButton uiButton3;
        private Sunny.UI.UIButton uiButton4;
        private Sunny.UI.UITextBox uiTextBox1;
    }
}

4:相关的资源和源代码文件已经上传(包括相关代码合执行文件,如果需要已经处理好的直接下载2就可以,如果需要学习相关代码逻辑下载1即可。)
1:https://download.csdn.net/download/xwwwill/89675458
2:https://download.csdn.net/download/xwwwill/89675414

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值