将泛型列表List转换为指定属性的XML

一般的 对象序列化成对象都是这样的

<?xml version="1.0" encoding="utf-8" ?>
<BaseInfo>
  <Person>
    <Name>小明</Name>
    <Age>16</Age>
    <Books>
      <Book>
        <ISBN>123</ISBN>
        <Title>借的书1</Title>
      </Book>
    </Books>
  </Person>
  <Person>
    <Name>小红</Name>
    <Age>18</Age>
    <Books>
      <Book>
        <ISBN>456</ISBN>
        <Title>借的书2</Title>
      </Book>
      <Book>
        <ISBN>789</ISBN>
        <Title>借的书3</Title>
      </Book>
    </Books>
  </Person>
</BaseInfo>

这样的代码要求层次比较清晰,没办法序列化成这样

<?xml version="1.0" encoding="utf-8"?>
<datapacket wsid="EDIS_SORESULT_ERP" errcode="" errmsg="">
  <rowsdata a="" b="" c="" />
</datapacket>

解决方案一:

通过反射和xml.linq来组装指定的对象:

 public static XDocument BuildXML<T>(List<T> data, string functionName = "")
        {
            XDocument xDocument = new XDocument();
            Type type = typeof(T);
            var properties = type.GetProperties();//获取属性
            XElement xElements = new XElement("datapacket",
                new XAttribute("wsid", functionName),
                new XAttribute("errcode", ""),
                new XAttribute("errmsg", ""),
                new XElement("rowsdata",
                data.Select(i => new XElement("row", properties.Select(p => new XAttribute(p.Name, p.GetValue(i)))
                ))));
            xDocument.Add(xElements);
            return xDocument;
        }

待反射对象:

public class EDIS_SORESULT_CFM_DTO
    {
        public string EDIS_SORESULT_SEQID { get; set; }
        public string EDIS_CONSIGNOR { get; set; }
        public string IMP_PROCESS_FLAG { get; set; }
    }
//初始化
 public List<EDIS_SORESULT_CFM_DTO> InitDemoList()
        {
            return new List<EDIS_SORESULT_CFM_DTO> {
                new EDIS_SORESULT_CFM_DTO {EDIS_CONSIGNOR="a1",EDIS_SORESULT_SEQID="a2",IMP_PROCESS_FLAG="a3" },
                new EDIS_SORESULT_CFM_DTO {EDIS_CONSIGNOR="b1",EDIS_SORESULT_SEQID="b2",IMP_PROCESS_FLAG="b3"  },
                new EDIS_SORESULT_CFM_DTO {EDIS_CONSIGNOR="c1",EDIS_SORESULT_SEQID="c2",IMP_PROCESS_FLAG="c3"  }
            };
        }

调用:

BuildXML(data, "EDIS_SORESULT_ERP")

 

预计返回结果:

<?xml version="1.0" encoding="utf-8"?>
<datapacket wsid="EDIS_SORESULT_ERP" errcode="" errmsg=""> <rowsdata> <row EDIS_SORESULT_SEQID="a2" EDIS_CONSIGNOR="a1" IMP_PROCESS_FLAG="a3" /> <row EDIS_SORESULT_SEQID="b2" EDIS_CONSIGNOR="b1" IMP_PROCESS_FLAG="b3" /> <row EDIS_SORESULT_SEQID="c2" EDIS_CONSIGNOR="c1" IMP_PROCESS_FLAG="c3" /> </rowsdata> </datapacket>

 

解决方案二:

直接在对象上加属性,但是这种方法只适合单个对象,如果是对象列表就不适合了

对象修改如下:

public class EDIS_SORESULT_CFM_DTO
    {
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string EDIS_SORESULT_SEQID { get; set; }
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string EDIS_CONSIGNOR { get; set; }
        public string IMP_PROCESS_FLAG { get; set; }
    }

 

具体代码如下:

 EDIS_SORESULT_CFM_DTO myObject = new EDIS_SORESULT_CFM_DTO { EDIS_CONSIGNOR = "a1", EDIS_SORESULT_SEQID = "a2", IMP_PROCESS_FLAG = "a3" };
            // Insert code to set properties and fields of the object.  
            XmlSerializer mySerializer = new
            XmlSerializer(typeof(EDIS_SORESULT_CFM_DTO));
            // To write to a file, create a StreamWriter object.  
            StreamWriter myWriter = new StreamWriter(@"d:/myFileName.xml");
            mySerializer.Serialize(myWriter, myObject);
            myWriter.Close();

返回结果:

<?xml version="1.0" encoding="utf-8"?>
<EDIS_SORESULT_CFM_DTO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EDIS_SORESULT_SEQID="a2" EDIS_CONSIGNOR="a1">
  <IMP_PROCESS_FLAG>a3</IMP_PROCESS_FLAG>
</EDIS_SORESULT_CFM_DTO>

 

参考:

c# - How to serialize a List<T> into XML? - Stack Overflow

XDocument简单入门 - hello*boy - 博客园 (cnblogs.com)

如何:序列化对象 | Microsoft Docs

上一篇:abpvnext密码强度及DTO的属性首字母保持小写设置


下一篇:jmeter压测dubbo接口,参数为dto时如何写传参及有错误时的分析思路