如何将c#Entity的标记与Xml相同

本文关键字:Xml 相同 c#Entity | 更新日期: 2023-09-27 18:22:23

我有问题!!

===============正常================

class trx()
{
   string trx_name;
   string type_id;
}
var 0 = new trx(){trx_name="1",trx_name="2"}

---实体更改为xml

[XmlSerializer serializer = new XmlSerializer(typeof(trx));
[serializer.Serialize(File.OpenWrite(@".'MyXml.xml"), o);]

----XML结果------

<trx>
  <trx_name>1</trx_name> 
  <type_id>2</type_id> 
</trx>

===============================

Q: 但是我需要trx XML

<trx>
  <trx_name>a</trx_name> 
  <trx_name>b</trx_name> 
  <trx_name>c</trx_name> 
  <trx_name>d</trx_name> 
</trx>

如何解决这个问题???

提前感谢您的帮助

如何将c#Entity的标记与Xml相同

类似的内容。

 public class trx
    {
        public string trx_name { get; set; }
    }
    public class CustomSerializer
    {
        private static void Write(string filename)
        {
            List<trx> trxs = new List<trx>();
            trxs.Add(new trx {trx_name = "Name1"});
            trxs.Add(new trx {trx_name = "Name2"});
            XmlSerializer x = new XmlSerializer(typeof (List<trx>));
            TextWriter writer = new StreamWriter(filename);
            x.Serialize(writer, trxs);
        }
        private static List<trx> Read(string filename)
        {
            var x = new XmlSerializer(typeof (List<trx>));
            TextReader reader = new StreamReader(filename);
            return (List<trx>) x.Deserialize(reader);
        }
    }
}