我想在C#
中将类序列化为XML我的课程:
[System.SerializableAttribute()]
[XmlRoot(Namespace = "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader")]
public class StandardBusinessDocument
{
public StandardBusinessDocumentHeader StandardBusinessDocumentHeader { get; set; }
public Package Package { get; set; }
}
[System.SerializableAttribute()]
[XmlRoot(Namespace = "http://www.efatura.gov.tr/package-namespace")]
public class Package
{
public Elements Elements { get; set; }
}
当我将这个类序列化为XML
时 XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("sh", "http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader");
namespaces.Add("ef", "http://www.efatura.gov.tr/package-namespace");
XmlSerializer xser = new XmlSerializer(typeof(StandardBusinessDocument));
xser.Serialize(soapwriter, new StandardBusinessDocument
{
StandardBusinessDocumentHeader = sbd,
Package = new Package()
{
Elements = elements
}
}, namespaces);
之后,我看到了第一个类( StandardBusinessDocumentHeader )并且在具有 sh前缀命名空间的节点下。但Pakacge总是显示 sh:前缀而不是 ef:,但另一方面显示的是 ef:前缀
我只想将 ef:前缀添加到 Pakage 节点
答案 0 :(得分:1)
您需要为Package
属性指定名称空间,以便Package
元素正确。
[XmlElement(Namespace = "http://www.efatura.gov.tr/package-namespace")]
public Package Package { get; set; }