C# Xml序列化后同名节点添加序号实现

354 阅读3分钟

背景

开发接口对接某平台,其要求的XML格式感觉很另类,如下所示:

<Program>
	<FunctionID>GB302</FunctionID>
	<feeinfo>
                <row1>
			<doctor_name>管理员</doctor_name>
		</row1>
		<row2>
			<doctor_name>管理员</doctor_name>
		</row2>
		<row3>
			<doctor_name>管理员</doctor_name>
		</row3>
	</feeinfo>
</Program>

出于对项目综合考虑,决定不使用Linq来实现,而使用C#基础的XmlSerializer。

实现

序列化与反序列化函数直接搬运前人现成的 www.cnblogs.com/fish-li/arc… 如下

public static class XmlUtils {
        private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding) {
            if (o == null)
                throw new ArgumentNullException("o");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            XmlSerializer serializer = new XmlSerializer(o.GetType());

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = false;
            settings.NewLineChars = "\r\n";
            settings.Encoding = encoding;
            //settings.IndentChars = "    ";

            // 不生成声明头
            settings.OmitXmlDeclaration = true;

            // 强制指定命名空间,覆盖默认的命名空间。
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);

            using (XmlWriter writer = XmlWriter.Create(stream, settings)) {
                serializer.Serialize(writer, o, namespaces);
                writer.Close();
            }
        }

        /// <summary>
        /// 将一个对象序列化为XML字符串
        /// </summary>
        /// <param name="o">要序列化的对象</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>序列化产生的XML字符串</returns>
        public static string XmlSerialize(object o, Encoding encoding) {
            using (MemoryStream stream = new MemoryStream()) {
                XmlSerializeInternal(stream, o, encoding);

                stream.Position = 0;
                using (StreamReader reader = new StreamReader(stream, encoding)) {
                    return reader.ReadToEnd();
                }
            }
        }

        /// <summary>
        /// 将一个对象按XML序列化的方式写入到一个文件
        /// </summary>
        /// <param name="o">要序列化的对象</param>
        /// <param name="path">保存文件路径</param>
        /// <param name="encoding">编码方式</param>
        public static void XmlSerializeToFile(object o, string path, Encoding encoding) {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");

            using (FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write)) {
                XmlSerializeInternal(file, o, encoding);
            }
        }

        /// <summary>
        /// 从XML字符串中反序列化对象
        /// </summary>
        /// <typeparam name="T">结果对象类型</typeparam>
        /// <param name="s">包含对象的XML字符串</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>反序列化得到的对象</returns>
        public static T XmlDeserialize<T>(string s, Encoding encoding) {
            if (string.IsNullOrEmpty(s))
                throw new ArgumentNullException("s");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(encoding.GetBytes(s))) {
                using (StreamReader sr = new StreamReader(ms, encoding)) {
                    return (T)mySerializer.Deserialize(sr);
                }
            }
        }

        /// <summary>
        /// 读入一个文件,并按XML的方式反序列化对象。InvalidOperationException: 不应有 <Program xmlns=''>。
        /// </summary>
        /// <typeparam name="T">结果对象类型</typeparam>
        /// <param name="path">文件路径</param>
        /// <param name="encoding">编码方式</param>
        /// <returns>反序列化得到的对象</returns>
        public static T XmlDeserializeFromFile<T>(string path, Encoding encoding) {
            if (string.IsNullOrEmpty(path))
                throw new ArgumentNullException("path");
            if (encoding == null)
                throw new ArgumentNullException("encoding");

            string xml = File.ReadAllText(path, encoding);
            return XmlDeserialize<T>(xml, encoding);
        }

        /// <summary>
        /// 将xml的对应节点追加序号,如多个row改为row1,row2
        /// </summary>
        /// <param name="xml">Xml文本</param>
        /// <param name="nodeName">要重命名的节点</param>
        /// <returns>加工后的xml</returns>
        public static string XmlRename(string xml,string nodeName) {C# 实现Xml序列化后同名节点添加序号
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            XmlNodeList xmlNodeList = doc.SelectNodes($"//{nodeName}");
            XmlNode lastParentNode = null;
            int i = 0;
            foreach(XmlElement elm in xmlNodeList) {
                XmlNode parentNode = elm.ParentNode;

                if(lastParentNode != parentNode) {
                    i = 1;
                }

                lastParentNode = parentNode;

                XmlElement newE = doc.CreateElement(nodeName + i++);
                
                //第1
                //foreach(XmlNode n in elm.ChildNodes) {
                //    newE.AppendChild(n);
                //}
                
                //第2
                //for(int j = 0; j < elm.ChildNodes.Count; j++) {
                //    newE.AppendChild(elm.ChildNodes [j]);
                //}
                
                //第3
                for(int j = elm.ChildNodes.Count-1; j >= 0; j--) {
                    newE.AppendChild(elm.ChildNodes [j]);
                }
                parentNode.AppendChild(newE);

                parentNode.RemoveChild(elm);

            }
            return doc.OuterXml;
        }
    }

其中XMlRename则是在技术老大指点下实现的,但是注意中间注释的这部分,我最开始使用第一种foreach得到结果为

<Program>
	<FunctionID>GB302</FunctionID>
	<feeinfo>
		<row1>
			<match_type/>
		</row1>
		<row2>
			<match_type/>
		</row2>
		<row3>
			<match_type/>
		</row3>
	</feeinfo>
</Program>

第二种for循环从小到大遍历得到结果为

<Program>
	<FunctionID>GB302</FunctionID>
	<feeinfo>
		<row1>
			<match_type/>
			<hosp_code/>
			<pill_item_code/>
			<factory/>
			<fee_date/>
			<price>0</price>
			<money>0</money>
			<input_staff/>
			<input_date/>
			<usage_freq/>
			<doctor_name>管理员</doctor_name>
		</row1>
		<row2>
			<match_type/>
			<hosp_code/>
			<pill_item_code/>
			<factory/>
			<fee_date/>
			<price>0</price>
			<money>0</money>
			<input_staff/>
			<input_date/>
			<usage_freq/>
			<doctor_name>管理员</doctor_name>
		</row2>
		<row3>
			<match_type/>
			<hosp_code/>
			<pill_item_code/>
			<factory/>
			<fee_date/>
			<price>0</price>
			<money>0</money>
			<input_staff/>
			<input_date/>
			<usage_freq/>
			<doctor_name>管理员</doctor_name>
		</row3>
	</feeinfo>
</Program>

第三种for循环从大到小遍历得到准确结果

<Program>
	<FunctionID>GB302</FunctionID>
	<feeinfo>
		<row1>
			<doctor_name>管理员</doctor_name>
			<doctor_no/>
			<usage_freq/>
			<dosage_explain/>
			<input_date/>
			<input_man/>
			<input_staff/>
			<recipe_id/>
			<money>0</money>
			<dosage>0</dosage>
			<price>0</price>
			<unit/>
			<fee_date/>
			<standard/>
			<factory/>
			<model/>
			<pill_item_code/>
			<hosp_name>医院</hosp_name>
			<hosp_code/>
			<stat_type/>
			<match_type/>
		</row1>
		<row2>
			<doctor_name>管理员</doctor_name>
			<doctor_no/>
			<usage_freq/>
			<dosage_explain/>
			<input_date/>
			<input_man/>
			<input_staff/>
			<recipe_id/>
			<money>0</money>
			<dosage>0</dosage>
			<price>0</price>
			<unit/>
			<fee_date/>
			<standard/>
			<factory/>
			<model/>
			<pill_item_code/>
			<hosp_name>医院</hosp_name>
			<hosp_code/>
			<stat_type/>
			<match_type/>
		</row2>
		<row3>
			<doctor_name>管理员</doctor_name>
			<doctor_no/>
			<usage_freq/>
			<dosage_explain/>
			<input_date/>
			<input_man/>
			<input_staff/>
			<recipe_id/>
			<money>0</money>
			<dosage>0</dosage>
			<price>0</price>
			<unit/>
			<fee_date/>
			<standard/>
			<factory/>
			<model/>
			<pill_item_code/>
			<hosp_name>医院</hosp_name>
			<hosp_code/>
			<stat_type/>
			<match_type/>
		</row3>
	</feeinfo>
</Program>

目前看来foreach只会遍历一个元素,for循环从小到大的话每次循环elm最后的子元素都会减少一个,导致总共21个属性循环完只能获取到11个。所以只能采用第三种了。

测试代码与实体

测试代码

    InputGB302 inputGB302 = new InputGB302();
    List<InputGB302FeeInfo> feeinfo = new List<InputGB302FeeInfo>();
    for(int i = 0; i < 3; i++) {
        InputGB302FeeInfo inputGB302FeeInfo = new InputGB302FeeInfo();
        inputGB302FeeInfo.hosp_name = "医院";
        inputGB302FeeInfo.doctor_name = "管理员";
        feeinfo.Add(inputGB302FeeInfo);
    }
    inputGB302.feeinfo = feeinfo;
    string s2 = XmlUtils.XmlSerialize(inputGB302, Encoding.Default);
    s2 = XmlUtils.XmlRename(s2, "row");    

实体

    [XmlRoot(ElementName = "Program")]
    public class InputGB302 : Input {
        [XmlArrayItem(ElementName ="row")]
        public List<InputGB302FeeInfo> feeinfo { get; set; }
    }
    
    public class InputGB302FeeInfo {
        public string match_type { get; set; } = "";

        public string stat_type { get; set; } = "";

        public string hosp_code { get; set; } = "";

        public string hosp_name { get; set; } = "";

        public string pill_item_code { get; set; } = "";

        public string model { get; set; } = "";

        public string factory { get; set; } = "";

        public string standard { get; set; } = "";

        public string fee_date { get; set; } = "";

        public string unit { get; set; } = "";

        public decimal price { get; set; }

        public decimal dosage { get; set; }

        public decimal money { get; set; }

        public string recipe_id { get; set; } = "";

        public string input_staff { get; set; } = "";

        public string input_man { get; set; } = "";

        public string input_date { get; set; } = "";

        public string dosage_explain { get; set; } = "";

        public string usage_freq { get; set; } = "";

        public string doctor_no { get; set; } = "";

        public string doctor_name { get; set; } = "";

    }