在 c# 中,如果我必须创建一个新的 XElement,我可以使用以下两种方法。
方法一
string m = "<address>";
m += "<addressline1>" + address.Addressline1 + "</addressline1>";
m += "<addressline2>" + address.Addressline2 + "</addressline2>";
m += "<city>" + address.Addressline1 + "</city>";
m += "<postcode>" + address.Addressline1 + "</postcode>";
m += "</address>";
方法二
XElement a = new XElement("address",
new XElement("addressline1", address.Addressline1),
new XElement("addressline2", address.Addressline2),
new XElement("city", address.City),
new XElement("postcode", address.postcode)
);
如果我的最终目标只是获取自定义对象作为 XML 字符串,哪种方法更可取?