与.NET XML序列化的附加元素包装序列化属性 [英] Wrapping Serialized Properties in Additional Elements with .NET XML Serialization

查看:129
本文介绍了与.NET XML序列化的附加元素包装序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用C#+ VSTS2008 + .NET 3.0做的XML序列化。在code正常工作。在这里,下面是我的code和当前的序列化的XML结果。

现在我要增加两个附加层到输出XML文件。这是我期望的XML结果。任何简单的方法来做到这一点?我不知道NestingLevel是否有助于做到这一点。我想找到不改变MyClass的和MyObject来的结构,一个简单的方法。

预期的XML序列化的结果,

 < XML版本=1.0&GT?;
< MyClass的的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance

的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
  < MyObjectProperty>
    < AdditionalLayer1>
      < AdditionalLayer2>
        <的ObjectName>富< /的ObjectName>
      < / AdditionalLayer1>
    < / AdditionalLayer2>
  < / MyObjectProperty>
< / MyClass的>
 

当前XML序列化的结果,

 < XML版本=1.0&GT?;
< MyClass的的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance

的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
  < MyObjectProperty>
    <的ObjectName>富< /的ObjectName>
  < / MyObjectProperty>
< / MyClass的>
 

我目前的code,

 公共类MyClass的
{
    公众为MyObject MyObjectProperty;
}
公共类为MyObject
{
    公共字符串的ObjectName;
}

公共类节目
{
    静态无效的主要(字串[] args)
    {
        XmlSerializer的S =新的XmlSerializer(typeof运算(MyClass的));
        的FileStream FS =新的FileStream(foo.xml,FileMode.Create);
        MyClass的实例=新MyClass的();
        instance.MyObjectProperty =新的MyObject();
        instance.MyObjectProperty.ObjectName =富;
        s.Serialize(FS,实例);

        返回;
    }
}
 

在此先感谢, 乔治

解决方案
  

我想找到不改变MyClass的和MyObject来的结构,一个简单的方法。

坦率地说,这是不会发生的。 的XmlSerializer (只需使用时)以下的类/构件的结构。所以你不能在不改变结构,增加额外的层。

当在一个定制的方式使用(的IXmlSerializable ),有一件事可以肯定的是,它不是简单的...这是不是一个有趣的界面实现。

我的建议是:引入一个DTO模仿你想要的格式,并垫片到您序列化之前

I am using C# + VSTS2008 + .Net 3.0 to do XML serialization. The code works fine. Here below is my code and current serialized XML results.

Now I want to add two additional layers to the output XML file. Here is my expected XML results. Any easy way to do this? I am not sure whether NestingLevel could help to do this. I want to find an easy way which does not change the structure of MyClass and MyObject.

Expected XML serialization result,

<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyObjectProperty>
    <AdditionalLayer1>
      <AdditionalLayer2>
        <ObjectName>Foo</ObjectName>
      </AdditionalLayer1>
    </AdditionalLayer2>
  </MyObjectProperty>
</MyClass>

Current XML serialization result,

<?xml version="1.0"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyObjectProperty>
    <ObjectName>Foo</ObjectName>
  </MyObjectProperty>
</MyClass>

My current code,

public class MyClass
{
    public MyObject MyObjectProperty;
}
public class MyObject
{
    public string ObjectName;
}

public class Program
{
    static void Main(string[] args)
    {
        XmlSerializer s = new XmlSerializer(typeof(MyClass));
        FileStream fs = new FileStream("foo.xml", FileMode.Create);
        MyClass instance = new MyClass();
        instance.MyObjectProperty = new MyObject();
        instance.MyObjectProperty.ObjectName = "Foo";
        s.Serialize(fs, instance);

        return;
    }
}

thanks in advance, George

解决方案

I want to find an easy way which does not change the structure of MyClass and MyObject.

Frankly, that isn't going to happen. XmlSerializer (when used simply) follows the structure of the classes / members. So you can't add extra layers without changing the structure.

When used in a bespoke way (IXmlSerializable), the one thing you can say for sure is that it isn't simple... this is not a fun interface to implement.

My advice: introduce a DTO that mimics your desired format, and shim to that before you serialize.

这篇关于与.NET XML序列化的附加元素包装序列化属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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