使用 Xmlnclude 后如何删除不需要的属性?
How to remove unwanted attributes after using Xmlnclude?
我正在尝试连载到以下 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
属性?
我有一个摘要 class AParty
有两个子classes ChargeParty
和 SenderParty
.
在我的 ManifestHeader
class 我有:
[XmlElement("Party")]
public AParty[] Parties;
我使用XmlInclude
如下:
[XmlInclude(typeof(PickupParty))]
[XmlInclude(typeof(SenderParty))]
在我的序列化程序中,我使用自定义命名空间:
serialiser.Serialize(file, this, nameSpace);
有什么想法吗?
编辑
我在发布问题之前查看了 that question。首先,我使用的是 XmlSerializer
而不是 DataContractSerializer
,其次,我已经成功地为我的所有对象设置了名称空间 除了 我已经包含在 XmlInclude
。因此这个问题。
这是一种变通方法,并不是真正的解决方案。
我没有使用基础 class 和子 classes,而是只使用了一个大的 class,这避免了对 XmlInclude
的需要,从而避免了不需要的属性(d4p1:type
和 xmlns:d4p1
)。
我现在用的单大class基本上综合了之前子class的所有属性。根据 class 的角色,仅使用一部分属性。
这很丑陋,所以如果有人有合适的解决方案,那将是卑鄙的!
属性 "{http://www.w3.org/2001/XMLSchema-instance}type"
, which usually appears with the prefix "xsi:type
", is a standard way for a polymorphic element to explicitly assert its type. XmlSerializer
uses it to 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)
constructor, one must 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>
我正在尝试连载到以下 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
属性?
我有一个摘要 class AParty
有两个子classes ChargeParty
和 SenderParty
.
在我的 ManifestHeader
class 我有:
[XmlElement("Party")]
public AParty[] Parties;
我使用XmlInclude
如下:
[XmlInclude(typeof(PickupParty))]
[XmlInclude(typeof(SenderParty))]
在我的序列化程序中,我使用自定义命名空间:
serialiser.Serialize(file, this, nameSpace);
有什么想法吗?
编辑
我在发布问题之前查看了 that question。首先,我使用的是 XmlSerializer
而不是 DataContractSerializer
,其次,我已经成功地为我的所有对象设置了名称空间 除了 我已经包含在 XmlInclude
。因此这个问题。
这是一种变通方法,并不是真正的解决方案。
我没有使用基础 class 和子 classes,而是只使用了一个大的 class,这避免了对 XmlInclude
的需要,从而避免了不需要的属性(d4p1:type
和 xmlns:d4p1
)。
我现在用的单大class基本上综合了之前子class的所有属性。根据 class 的角色,仅使用一部分属性。
这很丑陋,所以如果有人有合适的解决方案,那将是卑鄙的!
属性 "{http://www.w3.org/2001/XMLSchema-instance}type"
, which usually appears with the prefix "xsi:type
", is a standard way for a polymorphic element to explicitly assert its type. XmlSerializer
uses it to 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)
constructor, one must 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>