[英]Generate xml from c# class
I appreciate if someone could help me construct an XML in the following format: 如果有人可以帮助我构造以下格式的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>
This is what I have got so far: 到目前为止,这是我得到的:
<?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>
Here is the my code that I use to construct the xml 这是我用来构造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;
My Request class 我的要求班
[XmlRoot("request")]
public class Request
{
public Request()
{
zohorow = new List<ZohoVM>();
}
[XmlElement("row")]
public List<ZohoVM> zohorow { get; set; }
}
And my ZohoVm class 还有我的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; }
}
What I really want is to have "fl value" within in the xml tags. 我真正想要的是在xml标记中包含“ fl值”。 Thanks in advance 提前致谢
I fear the XmlSerializer
is not as flexible as you wish. 我担心XmlSerializer
不够灵活。 What you can do, though, is to extract an XML-Schema from your example XML which you want to achieve. 但是,您可以做的是从要实现的示例XML中提取XML-Schema。 Using the above, one can for example write the following schema: 使用上述内容,例如可以编写以下架构:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="requests" type="Request"/>
<xs:complexType name="RowFlag">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="val" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:complexType name="Row">
<xs:sequence>
<xs:element type="RowFlag" name="fl" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
<xs:attribute type="xs:byte" name="no"/>
</xs:complexType>
<xs:complexType name="Request">
<xs:sequence>
<xs:element type="Row" name="row"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Perhaps you can refine this schema according to your needs. 也许您可以根据需要优化此架构。 Then, you can generate the classes that create this schema using xsd.exe with the /classes
option. 然后,可以使用带有/classes
选项的xsd.exe生成用于创建此架构的/classes
。 In the generated code, you can add convenience properties which you then have to mark with an XmlIgnore
-attribute. 在生成的代码中,可以添加便捷属性,然后必须使用XmlIgnore
-attribute进行标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.