【发布时间】:2016-10-31 22:14:01
【问题描述】:
如果有人可以帮助我构建以下格式的 XML,我将不胜感激:
<requests>
<row no="1">
<fl val="Subject">Add Records Demo</fl>
<fl val="ContactName">John</fl>
<fl val="ProductName">Customer Care</fl>
<fl val="Email">john@demo.com</fl>
<fl val="Phone">002200330044</fl>
</row>
</requests>
这是我目前得到的:
<?xml version="1.0" encoding="utf-8"?>
<request>
<row>
<Subject>Add Records Demo</Subject>
<ContactName>John</ContactName>
<ProductName>Customer Care</ProductName>
<Email>john@demo.com</Email>
<Phone>002200330044</Phone>
</row>
</request>
这是我用来构建 xml 的代码
List<ZohoVM> lzr = new List<ZohoVM>();
lzr.Add(zvm);
Request rp = new Request();
rp.zohorow = lzr;
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer xsdocument = new XmlSerializer(typeof(Request));
StringWriter sw = new Utf8StringWriter();
string xml = "";
using (XmlWriter writer = XmlWriter.Create(sw))
{
xsdocument.Serialize(writer, rp, ns);
xml = sw.ToString();
}
return xml;
我的请求类
[XmlRoot("request")]
public class Request
{
public Request()
{
zohorow = new List<ZohoVM>();
}
[XmlElement("row")]
public List<ZohoVM> zohorow { get; set; }
}
还有我的 ZohoVm 类
public class ZohoVM
{
public string Subject { get; set; }
public string ContactName { get; set; }
public string ProductName { get; set; }
public string Email { get; set; }
public int Phone { get; set; }
}
我真正想要的是在 xml 标记中包含“fl 值”。提前致谢
【问题讨论】:
-
既然你非常关心格式,为什么要使用序列化而不是 XmlDocument/Xdocument?
标签: c# .net xml xml-serialization