我尝试序列化到以下XML:
...
<ManifestHeader>
<Party Role="CHARGE">
<f:Name>Name1</f:Name>
...
</Party>
<Party Role="SENDER">
<Name>Name2</Name>
...
</Party>
</ManifestHeader>
...
但是我得到了一些不需要的属性:
<ManifestHeader>
<Party d4p1:type="PickupParty" Role="CHARGE" xmlns:d4p1="http://www.w3.org/2001/XMLSchema-instance">
<f:Name>Name1</f:Name>
...
如何摆脱d4p1:type
和xmlns:d4p1
属性?
我有一个抽象类AParty
,有两个子类ChargeParty
和SenderParty
。
在我的ManifestHeader
课程中,我有:
[XmlElement("Party")]
public AParty[] Parties;
我使用XmlInclude
如下:
[XmlInclude(typeof(PickupParty))]
[XmlInclude(typeof(SenderParty))]
在我的序列化器中,我使用自定义命名空间:
serialiser.Serialize(file, this, nameSpace);
有什么想法吗?
修改
我在发布问题之前检查了that question。首先我使用XmlSerializer
而不是DataContractSerializer
,其次我已成功为我的所有对象设置名称空间,除了包含在XmlInclude
中。因此这个问题。
答案 0 :(得分:3)
属性"{http://www.w3.org/2001/XMLSchema-instance}type"
通常以前缀“xsi:type
”出现,是多态元素显式断言其类型的标准方式。 XmlSerializer
将其用于determine the type to deserialize - 因此在序列化期间将其写入以供日后使用。
在保持每个多态类型的相同元素名称的同时,没有简单的方法来抑制类型的输出。 (如果AParty
的每个子类都有不同的元素名称,那将很容易,但你没有。)最好的选择是ManifestHeader
实现IXmlSerializable
。您没有完全指定ManifestHeader
,因此请考虑以下示例:
[XmlRoot("ManifestHeader", Namespace = ManifestHeader.XmlNamespace)]
public class ManifestHeader : IXmlSerializable
{
public const string XmlNamespace = "MyNamespace";
public static XmlSerializerNamespaces GetXmlSerializerNamespaces()
{
var ns = new XmlSerializerNamespaces();
ns.Add("", ManifestHeader.XmlNamespace);
return ns;
}
// Some example properties
public string AProperty { get; set; }
public string ZProperty { get; set; }
// The list of parties.
public AParty[] Parties { get; set; }
#region IXmlSerializable Members
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
throw new NotImplementedException();
}
void IXmlSerializable.WriteXml(XmlWriter writer)
{
var ns = GetXmlSerializerNamespaces();
writer.WriteElementString("ZProperty", ZProperty);
foreach (var value in Parties)
{
XmlSerializationHelper.SerializeElementTo(value, "Party", ManifestHeader.XmlNamespace, writer, ns);
}
writer.WriteElementString("AProperty", AProperty);
}
#endregion
}
public abstract class AParty
{
[XmlAttribute]
public abstract string Role { get; set; } // Returns a constant string; setter does nothing.
}
这会手动序列化ManifestHeader
(如果有)的属性,循环遍历Party
数组的元素并序列化每个元素,并用"Party"
替换它们的元素名称。
它使用以下辅助方法。请注意,如果要使用XmlSerializer(Type, XmlRootAttribute)
构造函数覆盖根元素名称,则一个必须 cache the serializer in a hash table to avoid a horrible memory leak:
public static class XmlSerializationHelper
{
public static void SerializeElementTo<T>(T value, string elementName, string elementNamespace, XmlWriter writer, XmlSerializerNamespaces ns)
{
var serializer = XmlSerializerRootAttributeCache.DemandSerializer(value.GetType(), elementName, elementNamespace);
serializer.Serialize(writer, value, ns);
}
public static string GetXml<T>(this T obj)
{
return GetXml(obj, false);
}
public static string GetXml<T>(this T obj, bool omitNamespace)
{
return GetXml(obj, new XmlSerializer(obj.GetType()), omitNamespace);
}
public static string GetXml<T>(this T obj, XmlSerializer serializer)
{
return GetXml(obj, serializer, false);
}
public static string GetXml<T>(T obj, XmlSerializer serializer, bool omitStandardNamespaces)
{
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
{
ns = new XmlSerializerNamespaces();
ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
}
return GetXml(obj, serializer, ns);
}
public static string GetXml<T>(T obj, XmlSerializer serializer, XmlSerializerNamespaces ns)
{
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " }; // For cosmetic purposes.
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
serializer.Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
}
}
public static string GetXml<T>(this T obj, XmlSerializerNamespaces ns)
{
return GetXml(obj, new XmlSerializer(obj.GetType()), ns);
}
}
public static class XmlSerializerRootAttributeCache
{
readonly static Dictionary<Tuple<Type, string, string>, XmlSerializer> cache;
readonly static object padlock = new object();
static XmlSerializerRootAttributeCache()
{
cache = new Dictionary<Tuple<Type, string, string>, XmlSerializer>();
}
static XmlSerializer CreateSerializer(Type rootType, string rootName, string rootNamespace)
{
return new XmlSerializer(rootType, new XmlRootAttribute { ElementName = rootName, Namespace = rootNamespace });
}
public static XmlSerializer DemandSerializer(Type rootType, string rootName, string rootNamespace)
{
var key = Tuple.Create(rootType, rootName, rootNamespace);
lock (padlock)
{
XmlSerializer serializer;
if (!cache.TryGetValue(key, out serializer))
serializer = cache[key] = CreateSerializer(rootType, rootName, rootNamespace);
return serializer;
}
}
}
这是一个简单的测试用例:
[XmlRoot("ChargeParty", Namespace = ManifestHeader.XmlNamespace)]
[XmlType("ChargeParty", Namespace = ManifestHeader.XmlNamespace)]
public sealed class ChargeParty : AParty
{
[XmlAttribute]
public override string Role
{
get
{
return "CHARGE";
}
set
{
}
}
public bool IsCharging { get; set; }
}
[XmlRoot("SenderParty", Namespace = ManifestHeader.XmlNamespace)]
[XmlType("SenderParty", Namespace = ManifestHeader.XmlNamespace)]
public sealed class SenderParty : AParty
{
[XmlAttribute]
public override string Role
{
get
{
return "SENDER";
}
set
{
}
}
public string SenderName { get; set; }
}
public static class TestClass
{
public static void Test()
{
var manifest = new ManifestHeader
{
AProperty = "A property",
ZProperty = "Z Property",
Parties = new AParty[] { new SenderParty { SenderName = "Sender Name" }, new ChargeParty { IsCharging = true }, new SenderParty { SenderName = "Another Sender Name" }, new SenderParty { SenderName = "Yet Another Sender Name" }, new ChargeParty { IsCharging = false } }
};
var xml = manifest.GetXml(ManifestHeader.GetXmlSerializerNamespaces());
Debug.WriteLine(xml);
}
}
产生:
<ManifestHeader xmlns="MyNamespace"> <ZProperty>Z Property</ZProperty> <Party Role="SENDER"> <SenderName>Sender Name</SenderName> </Party> <Party Role="CHARGE"> <IsCharging>true</IsCharging> </Party> <Party Role="SENDER"> <SenderName>Another Sender Name</SenderName> </Party> <Party Role="SENDER"> <SenderName>Yet Another Sender Name</SenderName> </Party> <Party Role="CHARGE"> <IsCharging>true</IsCharging> </Party> <AProperty>A property</AProperty> </ManifestHeader>
答案 1 :(得分:1)
这是一种解决方法,而不是真正的解决方案。
我没有使用基类和子类,只使用了一个大类,这避免了XmlInclude
的需要,从而避免了不需要的属性(d4p1:type
和xmlns:d4p1
)。< / p>
我使用的单个大类基本上具有前面子类的所有属性。根据类的角色,仅使用属性的子集。
这非常难看,所以如果有人有一个合适的解决方案,那就是吝啬!