My first question on SO :,) and it's about XmlSerializer and namespace issue.
关于SO:)的第一个问题是关于XmlSerializer和命名空间的问题。
I know that there's already a lot of subject on how to remove the default Xml namespace from the root element of the Xml file and it's not the subject.
我知道,关于如何从Xml文件的根元素中删除默认的Xml名称空间,已经有很多主题了,而且它不是主题。
My question is how to remove it from child nodes when you're using derived classes ?
我的问题是在使用派生类时如何从子节点中删除它?
I've created my own serializer that can take custom namespaces or simply ignore them, and it's working well for the root element.
我已经创建了自己的序列化器,它可以使用自定义名称空间,也可以忽略它们,对于根元素来说,它工作得很好。
But when I use an abstract class to list some derived classes inside a List the serialization insert 2 attributes inside each derived classes' nodes.
但是当我使用抽象类在列表中列出一些派生类时,序列化会在每个派生类的节点中插入2个属性。
Like this :
是这样的:
<root>
<elements>
<element p3:type="XmlDerivedClass" xmlns:p3="{schema_url}" >
</element>
</elements>
</root>
As for my classes :
至于我的课程:
// Root element
[XmlRoot("root", Namespace="")]
public class XmlRootElement
{
List<XmlBaseClass> _Elements;
}
// Base class
[XmlInclude(typeof(XmlDerivedClass))] // Mandatory, prevents serialization errors
[XmlRoot(Namespace="")]
public abstract class XmlBaseClass
// Derived class
[XmlRoot("element", Namespace="")]
public class XmlDerivedClass : XmlBaseClass
I tried some common solutions :
我尝试了一些常见的解决方案:
- Using the Namespace="" attribute
- 使用名称空间= " "属性
- Implementing the XmlNamespaceDeclarations property (with the right empty Namespace)
- 实现xmlnamespacedeclaration属性(具有正确的空名称空间)
- Moving the XmlRoot() from base clase to derived one
- 将XmlRoot()从基类转移到派生类
- Changing XmlRoot() to XmlElement()
- XmlElement改变XmlRoot()()
I'll try to add the XmlInclude tag on the List to see if it changes something.
我将尝试在列表中添加XmlInclude标记,以查看它是否更改了某些内容。
So far, nothing worked to remove those damn namespaces...
到目前为止,还没有移除那些该死的命名空间……
If anyone has a solution for this, I'll be glad to try it.
如果有人能解决这个问题,我很乐意尝试。
[EDIT 21/02/2014] Well, it seems that I'm the only one facing this issue. I'll use a simple string.Replace to remove the useless XML but that's quite dirty.
[编辑21/02/2014]看来我是唯一一个面对这个问题的人。我将使用一个简单的字符串。替换为删除无用的XML,但这相当脏。
PS : For the context, the tags aren't an issue for the parser on the other end BUT they're not needed so I'm looking for a way to remove them.
PS:对于上下文来说,标记在另一端对解析器来说不是问题,但是它们是不需要的,所以我正在寻找一种方法来删除它们。
PS2 : Sorry for the any spelling mistakes, English isn't my native language.
学生2:很抱歉我的拼写错误,英语不是我的母语。
2 个解决方案
#1
2
I am not sure whether this is a solution which satisfies your needs because it's not really nice but it works and it doesn't seem too dirty.
我不确定这是不是一个能满足你需求的解决方案,因为它不是很好,但它确实有效,而且看起来也不太脏。
public abstract class Abs
{
public int Data { get; set; }
}
public class A : Abs{}
public class B : Abs{}
[Serializable]
[XmlRoot(elementName: "name")]
public class Ser
{
[XmlElement(elementName: "A")]
public List<A> AList { get; set; }
[XmlElement(elementName: "B")]
public List<B> BList { get; set; }
[XmlIgnore]
public List<Abs> AbsList {
get
{
var list = new List<Abs>(AList.ConvertAll(x=>(Abs)x));
list.AddRange(BList.ConvertAll(x=>(Abs)x));
return list;
}
}
}
Instead of XmlInclude you can create a List with derived-class objects and afterwards add them together or maybe cache them within a private member. Please notice again this is still far from ideal but it works for me so i wanted to share it.
与XmlInclude不同,您可以创建一个带有derived类对象的列表,然后将它们添加到一起,或者在一个私有成员中缓存它们。请再次注意,这仍然远不是理想的,但它对我很有效,所以我想分享它。
#2
0
If you add [XmlType]
, you can give the serializer the information about the type, such as the namespace it belongs to:
如果您添加[XmlType],您可以向序列化器提供关于类型的信息,例如它所属的名称空间:
// Base class
[XmlInclude(typeof(XmlDerivedClass))] // Mandatory, prevents serialization errors
[XmlType(Namespace="")]
public abstract class XmlBaseClass
// Derived class
[XmlType("element", Namespace="")]
public class XmlDerivedClass : XmlBaseClass
#1
2
I am not sure whether this is a solution which satisfies your needs because it's not really nice but it works and it doesn't seem too dirty.
我不确定这是不是一个能满足你需求的解决方案,因为它不是很好,但它确实有效,而且看起来也不太脏。
public abstract class Abs
{
public int Data { get; set; }
}
public class A : Abs{}
public class B : Abs{}
[Serializable]
[XmlRoot(elementName: "name")]
public class Ser
{
[XmlElement(elementName: "A")]
public List<A> AList { get; set; }
[XmlElement(elementName: "B")]
public List<B> BList { get; set; }
[XmlIgnore]
public List<Abs> AbsList {
get
{
var list = new List<Abs>(AList.ConvertAll(x=>(Abs)x));
list.AddRange(BList.ConvertAll(x=>(Abs)x));
return list;
}
}
}
Instead of XmlInclude you can create a List with derived-class objects and afterwards add them together or maybe cache them within a private member. Please notice again this is still far from ideal but it works for me so i wanted to share it.
与XmlInclude不同,您可以创建一个带有derived类对象的列表,然后将它们添加到一起,或者在一个私有成员中缓存它们。请再次注意,这仍然远不是理想的,但它对我很有效,所以我想分享它。
#2
0
If you add [XmlType]
, you can give the serializer the information about the type, such as the namespace it belongs to:
如果您添加[XmlType],您可以向序列化器提供关于类型的信息,例如它所属的名称空间:
// Base class
[XmlInclude(typeof(XmlDerivedClass))] // Mandatory, prevents serialization errors
[XmlType(Namespace="")]
public abstract class XmlBaseClass
// Derived class
[XmlType("element", Namespace="")]
public class XmlDerivedClass : XmlBaseClass