XML 反序列化:对象有默认值 [英] XML deserialization: object has default values

查看:38
本文介绍了XML 反序列化:对象有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我的 XML 序列化程序出现了奇怪的行为.
读取 XML 并将其反序列化为对象后,所有属性都设置为其默认值,而不是 xml 文件中声明的值.
序列化程序不会抛出异常并正常运行.xml 文件格式正确并适合类结构.
任何人都知道这是怎么回事,或者我如何找到问题的根源?
谢谢


I've got a strange behaviour with my XML serializer.
After reading an XML and deserializing it into an object, all properties are set to it's default values and not to the values declared in the xml file.
The serializer doesn't throw an exception and runs properly. The xml file is properly formed and fits to the class structure.
Anyone an idea how that can be, or how I could get to the source of the problem?
Thank you

我没有告诉你整个故事.问题是,我得到的 XML 来自另一个组件.我能够反序列化 XML 文件,现在我得到了不同的格式.由于该文件有大约 3000 行,我无法发布整个代码.但区别在于:
可反序列化:

edit: I didn't tell you the whole story. The thing is, the XML which I get is from another component. I was able to deserialize the XML file and now I got a diffrent format. Since the file has about 3000 lines I can't post the whole code. But here's the difference:
deserializable:

<?xml version="1.0" encoding="utf-8"?>

<Prop1 xmlns="">6</Prop1>
<Prop2 xmlns="">string</Prop2>

不可反序列化

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

6
string

我真的不明白为什么我可以解组第一个示例,因为 每个 元素中的 xmlns 标记以及为什么我不能解组第二个...

I don't really understand why I can unmarshal the first example due to the xmlns tag inside each element and why I can't unmarshal the second one...

edit2: 刚刚意识到只有顶级元素才有这些奇怪的 xmlns="" 属性.但是 C# 类声明与所有其他类没有什么不同......这很奇怪.

edit2: just realized that just the top level elements got these strange xmlns="" attributes. But the C# class declaration is not different from all other classes...that's weird.

C# 类是这样的:

using namespace1;

namespace namespace3
{
  [System.SerializableAttribute()]
  [System.ComponentModel.DesignerCategoryAttribute("code")]
  [System.Xml.Serialization.XmlTypeAttribute(Namespace="namespace3")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace="namespace3", IsNullable=true)]
  public partial class rootElem: BaseObject
  {
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public int Prop1
    {
      //...
    }
  }
}

推荐答案

一个具体的例子(包括 c# 和 xml)在这里会有很长的路要走.最有可能的一种:

A concrete example (both c# ad xml) would go a very long way here. Most likely one of:

  • 名称不是 xml 和 c# 之间的精确(区分大小写)匹配(允许通过属性等覆盖名称)
  • xml 属性/元素之间存在混淆
  • xml 命名空间存在差异(通常存在于 xml 中,而在 c# 中缺失)

通过您的编辑,它变得更加清晰.Xml 命名空间非常重要;<foo xmlns="abc"/><foo/> 完全无关.此外,xml 命名空间是继承的,所以在:

With your edit, it becomes clearer. Xml namespaces are very significant; <foo xmlns="abc"/> and <foo/> are completely unrelated. Further, xml namespaces are inherited, so in:

<rootElem xmlns:cfg="namespace1" xmlns:office="namespace2" xmlns="namespace3">
<Prop1>6</Prop1>
<Prop2>string</Prop2>
</rootElem>

Prop1Prop2 位于它们从父级继承的 namespace3 命名空间中.为了让 c# 绝对清楚您希望它们位于子命名空间(而不是空命名空间)中,告诉它:

it is the case that Prop1 and Prop2 are in the namespace3 namespace that they inherit from their parent. To make it absolutely clear to the c# that you want those to be in the child namespace (rather than the empty namespace), tell it:

[Serializable]
[DesignerCategory("code")]
[XmlType(Namespace = Namespace3)]
[XmlRoot(Namespace = Namespace3, IsNullable = true)]
public partial class rootElem
{
    private const string Namespace3 = "namespace3"; // to avoid repetition

    [XmlElement(Namespace = Namespace3)]
    public int Prop1 { get; set; }
}

这篇关于XML 反序列化:对象有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆