XMLSerialize类定义 [英] XMLSerialize class definition

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

问题描述

我知道我以前见过这个,但我不记得如何用它的定义来序列化一个类

。例如,我希望每个类不仅要序列化

,还要包含字段数据类型的字段数据类型。


我''现在使用XmlSerializer。


谢谢,

Joe

I know I''ve seen this before but I can''t remember how to serialize a class
with it''s definition. For example I want each class to serialize not only
with it''s fields'' values but the fields data type as well.

I''m using XmlSerializer now.

Thanks,
Joe

推荐答案

这里这是一个例子,来自我写的一个应用程序:


public void Serialize(string filePath,bool overwrite)

{

if(File.Exists(filePath)&&!overwrite)

抛出新的IOException(文件+文件路径+

存在。参数\\ \\overwrite\必须为true才能覆盖);

尝试

{

using(FileStream fs = new FileStream( filePath,FileMode.Create))

{

XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));

serializer.Serialize(fs,这个);

}

}

catch(例外情况)

{

Utilities.HandleError(例如,fil ePath:" + filePath);

throw;

}

}


-

HTH,


Kevin Spencer

微软MVP

专业鸡肉沙拉炼金术士


你所寻求的是你得到的。

" Joe" < jb ******* @ noemail.noemailwrote in message

news:uO ************** @ TK2MSFTNGP04.phx.gbl ...
Here''s an example, from an app I wrote:

public void Serialize(string filePath, bool overwrite)
{
if (File.Exists(filePath) && !overwrite)
throw new IOException("File " + filePath +
"exists. Parameter \"overwrite\" must be true to overwrite");
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));
serializer.Serialize(fs, this);
}
}
catch (Exception ex)
{
Utilities.HandleError(ex, "filePath: " + filePath);
throw;
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"Joe" <jb*******@noemail.noemailwrote in message
news:uO**************@TK2MSFTNGP04.phx.gbl...

>我知道我以前见过这个,但我不记得如何用它的定义来序列化一个类
。例如,我希望每个类不仅要序列化它的'字段'值,还要字段数据类型。


我现在正在使用XmlSerializer 。


谢谢,

Joe
>I know I''ve seen this before but I can''t remember how to serialize a class
with it''s definition. For example I want each class to serialize not only
with it''s fields'' values but the fields data type as well.

I''m using XmlSerializer now.

Thanks,
Joe



嗨Kevin,


我已经这样做了但它没有序列化每个字段的数据类型

成员被序列化只是字段的名称和价值。


-JO


" Kevin Spencer" < uc *@ftc.govwrote in message

news:eo ************* @ TK2MSFTNGP02.phx.gbl ...
Hi Kevin,

I''m already doing this but it doesn''t serialize the data type of each field
member being serialized just the fields'' name and value.

-Joe

"Kevin Spencer" <uc*@ftc.govwrote in message
news:eo*************@TK2MSFTNGP02.phx.gbl...

这是一个例子,来自我写的一个应用程序:


public void Serialize(string filePath,bool overwrite)

{

if(File.Exists(filePath)&&!overwrite)

抛出新IOException(" File" + filePath +

" exists。参数\overwrite \必须为true才能覆盖);

尝试

{

using(FileStream fs = new FileStream(filePath,FileMode.Create))

{

XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));

serializer.Serialize(fs,this);

}

}

catch(exception ex)

{

Utilities.HandleError(例如,filePath:" + filePath);

throw;

}

}


-

HTH,


Kevin Spen cer

微软MVP

专业鸡肉沙拉炼金术士


你所寻求的是什么。


" Joe" < jb ******* @ noemail.noemailwrote in message

news:uO ************** @ TK2MSFTNGP04.phx.gbl ...
Here''s an example, from an app I wrote:

public void Serialize(string filePath, bool overwrite)
{
if (File.Exists(filePath) && !overwrite)
throw new IOException("File " + filePath +
"exists. Parameter \"overwrite\" must be true to overwrite");
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));
serializer.Serialize(fs, this);
}
}
catch (Exception ex)
{
Utilities.HandleError(ex, "filePath: " + filePath);
throw;
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"Joe" <jb*******@noemail.noemailwrote in message
news:uO**************@TK2MSFTNGP04.phx.gbl...

>>我知道我以前见过这个,但我不记得如何用它来序列化一个类
定义。例如,我希望每个类不仅要序列化它的'字段'值,还要字段数据类型。

我现在正在使用XmlSerializer。

谢谢,
Joe
>>I know I''ve seen this before but I can''t remember how to serialize a class
with it''s definition. For example I want each class to serialize not only
with it''s fields'' values but the fields data type as well.

I''m using XmlSerializer now.

Thanks,
Joe




你好乔,


对这个误会感到抱歉。在我的生活中,我不能想出为什么你会想要这样做。字段和属性的数据类型

在它们所在的类的模式中,类成员的类型

被省略,因为它们不需要解析文件,并不是标准要求的
。那就是说,如果你有理由想要这么做,我相信你可以创建一个自定义的XmlSerializer来实现这个目标。


-

HTH,


Kevin Spencer

Microsoft MVP

专业鸡肉沙拉炼金术士


你所寻求的是你所得到的。

" Joe" < jb ******* @ noemail.noemailwrote in message

news:************** @ TK2MSFTNGP05.phx.gbl ...
Hi Joe,

Sorry about the misunderstanding. I can''t, for the life of me, figure out
why you would want to do this. The data types of the fields and properties
is in the schema for the class they reside in, and types of class members
are omitted because they are not needed to parse the document, and are not
required by the standard. That said, and if you have a reason for wanting to
do so, I believe you could create a custom XmlSerializer to do this.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"Joe" <jb*******@noemail.noemailwrote in message
news:Of**************@TK2MSFTNGP05.phx.gbl...

嗨凯文,


我已经这样做但它并没有序列化每个数据类型

字段成员正在序列化字段的名称和值。


-Joe


" Kevin斯潘塞" < uc *@ftc.govwrote in message

news:eo ************* @ TK2MSFTNGP02.phx.gbl ...
Hi Kevin,

I''m already doing this but it doesn''t serialize the data type of each
field member being serialized just the fields'' name and value.

-Joe

"Kevin Spencer" <uc*@ftc.govwrote in message
news:eo*************@TK2MSFTNGP02.phx.gbl...

>这是一个例子,来自我写的一个应用程序:

public void Serialize(string filePath,bool overwrite)
{
if(File.Exists(filePath)&&!overwrite)
抛出新的IOException(文件+文件路径+
存在。参数\覆盖\"必须为true才能覆盖);
尝试
{
使用(FileStream fs = new FileStream(filePath,FileMode.Create))
{x / XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));
serializer.Serialize(fs,this);
}
}
catch(Exception ex)
{
实用工具.HandleError(例如,filePath:" + filePath);
throw;
}
}

-
HTH,
Kevin Spencer
微软MVP
专业鸡肉沙拉炼金术士

你所寻求的是什么。

" Joe& QUOT; < jb ******* @ noemail.noemailwrote in message
新闻:uO ************** @ TK2MSFTNGP04.phx.gbl ...
>Here''s an example, from an app I wrote:

public void Serialize(string filePath, bool overwrite)
{
if (File.Exists(filePath) && !overwrite)
throw new IOException("File " + filePath +
"exists. Parameter \"overwrite\" must be true to overwrite");
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer(typeof(PrinterDocument));
serializer.Serialize(fs, this);
}
}
catch (Exception ex)
{
Utilities.HandleError(ex, "filePath: " + filePath);
throw;
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"Joe" <jb*******@noemail.noemailwrote in message
news:uO**************@TK2MSFTNGP04.phx.gbl...

>>>我知道我之前见过这个,但我不记得如何用它的定义序列化一个
类。例如,我希望每个类不仅使用它的'field''值来序列化
,还要使用字段数据类型。

我现在正在使用XmlSerializer。

谢谢,
Joe
>>>I know I''ve seen this before but I can''t remember how to serialize a
class with it''s definition. For example I want each class to serialize
not only with it''s fields'' values but the fields data type as well.

I''m using XmlSerializer now.

Thanks,
Joe





这篇关于XMLSerialize类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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