带有基类问题的XML序列化 [英] Xml serialization with baseclass problem

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

问题描述

大家好!

我已经为这个问题苦苦挣扎了一段时间,但我只是不
得到它.在这里..

我有很多类都继承了提供
的相同基类. 将xml序列化到文件中.序列化可以完美运行,直到
类具有十进制属性,必须将其序列化为XmlTextAttribute .

我的课程很复杂,但我制作了一个简单的代码段,可以再现
完全一样的问题.

我的基础班

Hi All!

I have been strugling with this problem for some time and I just dont
get it. Here it goes..

I have many classess which all inherits same baseclass which offers
class xml serialization into the file. Serialization works perfectly until
class has decimal property which has to be serialized as XmlTextAttribute.

My classes are quite complex but I made simple snippet which reproduce
exactly same problem.

My base Class

using System.Text;
using System.Xml;
using System.ComponentModel;
using System.Xml.Serialization;

namespace SerializeDecimal
{
    public partial class BaseClass<T>
    {
        private XmlSerializer Serializer = new XmlSerializer(typeof(T));

        private string Serialize()
        {
            System.IO.StreamReader streamReader = null;
            System.IO.MemoryStream memoryStream = null;
            try
            {
                memoryStream = new System.IO.MemoryStream();
                System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
                xmlWriterSettings.Encoding = Encoding.UTF8;
                System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
                Serializer.Serialize(xmlWriter, this);
                memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                streamReader = new System.IO.StreamReader(memoryStream);
                return streamReader.ReadToEnd();
            }
            finally
            {
                if ((streamReader != null))
                {
                    streamReader.Dispose();
                }
                if ((memoryStream != null))
                {
                    memoryStream.Dispose();
                }
            }
        }

        public void SaveToFile(string fileName)
        {
            System.IO.StreamWriter streamWriter = null;
            try
            {
                string xmlString = Serialize();
                streamWriter = new System.IO.StreamWriter(fileName, false, Encoding.UTF8);
                streamWriter.WriteLine(xmlString);
                streamWriter.Close();
            }
            finally
            {
                if ((streamWriter != null))
                {
                    streamWriter.Dispose();
                }
            }
        }       
    }
}



继承基类的简单类



And simple class which inherits baseclass

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace SerializeDecimal
{
[Serializable]
    public class MyClass2 : BaseClass<MyClass2>
    {
        private decimal myDecimal;

        //[System.Xml.Serialization.XmlText(typeof(Decimal))]
        [System.Xml.Serialization.XmlTextAttribute(DataType = "decimal")]
        public decimal MyDecimal
        {
            get { return myDecimal; }
            set{myDecimal = value;}            
        }
    }
}



最后是代码中的类用法(例如,在buttonClick事件内部)



And finally class usage in code (eg. inside buttonClick event)

string filepath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "MyClass.xml");

            MyClass2 myClass = new MyClass2();
            myClass.MyDecimal = 12.3M;

            myClass.SaveToFile(filepath);



就像我在一开始所说的那样,在类具有十进制属性(需要将其序列化为XmlTextAttribute)之前,此方法非常有用.
我玩这个问题已经有一段时间了,发现有些奇怪的
事物

BaseClass似乎讨厌以下情况:

A)带有XmlTextAttribute的十进制属性(例如XmlElement正常,
但自然输出的不是XmlText)

B)如果我忘记了BaseClass的想法,只是实现了序列化到每个
类本身,即使具有十进制属性,它也可以正常工作
XmlTextAttribute就可以了!这不是选项,因为我将处理数百个
要序列化的类的集合,因此很高兴拥有常见的基类.

有人可以点点东西照亮这里是什么..

更新:

好的,一些输出文件上面属性的xml标签做什么

当属性描述为XmlElement
时输出



And like I said in the beginning this works great until class has decimal property which need to be serialized as XmlTextAttribute..

I have been playing with this problem some time and found out some weird
things

BaseClass seems to hate following scenarios:

A) decimal properties WITH XmlTextAttribute on it (eg. XmlElement is ok,
but naturally output is not XmlText )

B) If I forget BaseClass idea and just implemet serialization into each
class itself, it works EVEN it has decimal properties WITH
XmlTextAttribute on it! This is not option since I will handle hundreds
of classes to be serialized so common baseclass is nice to have.

Could someone kindly light things up littlebit what is bugging here..

UPDATE:

Ok, some output files what xml tags above properties do

this is output when property is described to XmlElement

<?xml version="1.0" encoding="utf-8"?>
<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<MyDecimal>12.3</MyDecimal>
</MyClass>



当将属性描述为XmlTextAttribute时将输出此结果(这是通缉的情况)



this is output when property is described to XmlTextAttribute (this is wanted scenario)

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





希望你有主意:)

干杯!





Hope you got idea :)

Cheers!

推荐答案

尝试将<T>放到基类中.使用this.GetType()代替XmlSerializer的定义.
这不一定与手头的问题有关,只是构造一个实例两次告诉它使用哪个类是错误的.
Try dropping the <T> in your base class. Use this.GetType() instead for XmlSerializer''s definition.
This doesn''t necessarily have to do with the problem at hand, it just feels wrong to construct an instance telling it twice what class to use.


这篇关于带有基类问题的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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