烦人的xml序列化的问题

jiangfeng999 2004-10-28 10:06:49
我写了一个xml序列化的C#程序,代码如下:
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using XmlDoc;

namespace 序列化与反序列化
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
XmlReader xr=new XmlTextReader(@"F:\VS.NET源代码\C#\XML\第十章 序列化xml\序列化与反序列化\purchaseOrder.xml");
StreamWriter sw=new StreamWriter(@"F:\VS.NET源代码\C#\XML\第十章 序列化xml\序列化与反序列化\output1.xml");
purchaseOrder po=new purchaseOrder();
XmlSerializer serializer=new XmlSerializer(typeof(purchaseOrder));
try
{
if(serializer.CanDeserialize(xr)==true)
po=(purchaseOrder)serializer.Deserialize(xr);
serializer.Serialize(sw,po);
}
catch(IOException ie)
{
MessageBox.Show(ie.Message+"\n"+ie.Source);
Application.Exit();
}
catch(Exception e)
{
MessageBox.Show(e.Message+"\n"+e.Source);
Application.Exit();
}
}
}
}
编译没问题,可在执行到XmlSerializer serializer=new XmlSerializer(typeof(purchaseOrder));
时总是跳出"未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。

其他信息: 找不到文件或程序集名称“gfradvjr.dll”,或找不到它的一个依赖项。"
这样的异常信息,而且每次的“找不到文件或程序集名称”都不同。
请各位DG们多多帮忙,谢谢了
...全文
217 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
happyjun2000 2004-10-30
  • 打赏
  • 举报
回复
未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。

主要是由于你的文件访问的设置不正确,你可以设置
Stream _stream = new FileStream ( "c:\\xmlPerson.xml" ,FileMode.Create ,FileAccess.Write ,FileShare.None ) ;


小例子:
public static void Serialization_ByXml ( )
{
Person _obj = new Person ( ) ;
_obj.Name = "Paladin" ;
_obj.Education = "doctor" ;
XmlSerializer s = new XmlSerializer ( typeof ( Person ) ) ;
Stream _stream = new FileStream ( "c:\\xmlPerson.xml" , FileMode.Create ,FileAccess.Write ,FileShare.None ) ;
s.Serialize( _stream , _obj ) ;
_stream.Close ( ) ;
}

public static void DeSerialization_ByXml ( )
{
Stream _stream = File.Open ( "c:\\xmlPerson.xml" , FileMode.Open ) ;

XmlSerializer _s = new XmlSerializer ( typeof ( Person ) ) ;
Person _person = ( Person ) _s.Deserialize ( _stream ) ;
_stream.Close ( ) ;

common.setMessage( _person.Name + _person.Education ) ;
}
}//class

//==========================================================================
//
// person Class
//
//==========================================================================
[Serializable]
public class Person
{
public string Name = "" ;
private int Age = 24 ;
public string Education = null ;
}
bborn 2004-10-29
  • 打赏
  • 举报
回复
在文档中心搜索可以找到一个例子
我试过的 挺好用的
jiangfeng999 2004-10-29
  • 打赏
  • 举报
回复
在这之前,我已经将purchaseOrder.xml生成purchaseOrder.xsd和purchaseOrder.cs文件
jiangfeng999 2004-10-29
  • 打赏
  • 举报
回复
purchaseOrder是xml文档的名字
<?xml version="1.0"?>
<purchaseOrder orderDate="1999-10-20">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Mill Valley</city>
<state>CA</state>
<zip>90952</zip>
</shipTo>
<billTo country="US">
<name>Robert Smith</name>
<street>8 Oak Avenue</street>
<city>Old Town</city>
<state>PA</state>
<zip>95819</zip>
</billTo>
<comment>Hurry, my lawn is going wild!</comment>
<items>
<item partNum="872-AA">
<productName>Lawnmower</productName>
<quantity>1</quantity>
<price>148.95</price>
<comment>Confirm this is electric</comment>
</item>
<item partNum="926-AA">
<productName>Baby Monitor</productName>
<quantity>1</quantity>
<price>39.98</price>
<shipDate>1999-05-21</shipDate>
</item>
</items>
</purchaseOrder>


BearRui 2004-10-28
  • 打赏
  • 举报
回复
see you purchaseOrder
lbx1979 2004-10-28
  • 打赏
  • 举报
回复
purchaseOrder 是什么
xiaohutushen 2004-10-28
  • 打赏
  • 举报
回复
up

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧