[英]Serialize Object to XML in with specific format
Im trying to Serialize an object( a class in this case) with an specific fomat. 我试图用一个特定的fomat序列化一个对象(在这种情况下是一个类)。 I got something like this:
我有这样的事情:
<ns:pay xmlns:ns="http://example.uri.here">
<ns:Payment>
<ns:customerKeyValue>5555</ns:customerKeyValue>
<ns:bankCode>BBBB</ns:bankCode>
<ns:paymentAmount>456</ns:paymentAmount>
<ns:paymentCategory>KD</ns:paymentCategory>
<ns:paymentMode>AC</ns:paymentMode>
<ns:referenceNumber>123A</ns:referenceNumber>
<ns:userID>Test2</ns:userID>
<ns:invoiceNumber>61</ns:invoiceNumber>
</ns:Payment>
</ns:pay>
I have the class that have each element but when i serialize it it convert its to this format: 我有一个具有每个元素的类,但是当我序列化它时,它将其转换为这种格式:
<?xml version="1.0"?>
<ns_x003A_pay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.uri.here">
<CustomerKeyValue>5555</CustomerKeyValue>
<BankCode>BBBB</BankCode>
<PaymentAmount>456</PaymentAmount>
<PaymentCategory>KD</PaymentCategory>
<PaymentMode>AC</PaymentMode>
<ReferenceNumber>123A</ReferenceNumber>
<UserID>Test2</UserID>
<InvoiceNumber>61</InvoiceNumber>
</ns_x003A_pay>
So anyone can help me with that? 所以任何人都可以帮助我吗? The method that im using to convert to xml is this:
我用来转换为xml的方法是这样的:
public static string SerializeToXMLString(object ObjectToSerialize)
{
MemoryStream mem = new MemoryStream();
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType());
ser.Serialize(mem, ObjectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(mem.ToArray());
}
note: to specify the namespace and class name i'm using this: [XmlRootAttribute( "ns:pay", Namespace = " http://example.uri.here ")] in the class 注意:要指定命名空间和类名,我正在使用它:[XmlRootAttribute(“ns:pay”,Namespace =“ http://example.uri.here ”)]在课堂上
If you haven't noted every XML element start with 如果您没有注意到每个XML元素都以
Thanks for you help. 谢谢你的帮助。
Ok guys I just found the answer here to my questions and i'm writing here to help people with this problem: 好的,我刚刚在这里找到了我的问题的答案,我写这里是为了帮助解决这个问题的人:
public static string SerializeToXMLString(object ObjectToSerialize)
{
//
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ns", "http://example.uri.here");
//
//
XmlSerializer serializer = new XmlSerializer(ObjectToSerialize.GetType());
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
serializer.Serialize(xmlWriter, ObjectToSerialize,ns);
}
return stringWriter.ToString();
}
To solve the prefix : I created a XmlSerializerNamespaces object and added the prefix that I wanted and the namespace. 要解决前缀:我创建了一个XmlSerializerNamespaces对象,并添加了我想要的前缀和命名空间。
To solve the ns:pay ns:payment I created two classes: Payment and Pay. 要解决ns:支付ns:支付我创建了两个类:支付和支付。
In the pay class i added this: [XmlRoot("pay", Namespace = " http://example.uri.here ")] 在pay类中我添加了这个:[XmlRoot(“pay”,Namespace =“ http://example.uri.here ”)]
In the Payment Class i added this: [XmlRoot("pay")] 在付款类我添加了这个:[XmlRoot(“pay”)]
Pay Class has a property of type payment. Pay Class有付款类型的财产。 That create the xml in this style:
这样就创建了这个样式的xml:
<ns:pay>
<ns:payment
element here
</ns:payemnt>
</ns:pay>
Thank you guys. 感谢大伙们。 Sorry that I ask and found the question almost 30 minutes after asking.
对不起,我询问并在询问后差不多30分钟找到了这个问题。
还有LINQtoXSD (不是XmlSerializer,当然:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.