序列化和反序列化

后浪 后浪 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
SiteConfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<xmlroot>
<shoptemplate text="大众点评" myspace="dzdp" class="dzdp.CInfoCls">
<channel text="咖啡厅">search/category/4/30/g2027</channel>
<channel text="酒吧">search/category/4/30/g2029</channel>
<channel text="茶馆">search/category/4/30/g2025</channel>
<channel text="KTV">search/category/4/30/g4807</channel>
<channel text="电影院">search/category/4/30/g4782</channel>
<channel text="文化艺术">search/category/4/30/g4755</channel>
<channel text="景点/郊游">search/category/4/30/g4784</channel>
<channel text="公园">search/category/4/30/g4802</channel>
<channel text="足疗按摩">search/category/4/30/g4793</channel>
<channel text="桑拿水会">search/category/4/30/g4778</channel>
<channel text="游乐游艺">search/category/4/30/g4788</channel>
<channel text="桌面游戏">search/category/4/30/g6696</channel>
<channel text="桌球室">search/category/4/30/g24677</channel>
<channel text="DIY手工坊">search/category/4/30/g4762/g30g4762</channel>
</shoptemplate>
<newstemplate text="瑞丽女性网" myspace="rayli" class="rayli.CInfoCls">
<channel text="瑞丽夜店">zixun/LT_407604.html</channel>
<channel text="瑞丽酒吧">zixun/LT_379668.html</channel>
</newstemplate>
</xmlroot>

siteconfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Web;

namespace DME.Collector
{
/// <summary>
/// 配置文件SiteConfig.xml
/// </summary>
public static class SiteConfig
{
/// <summary>
/// 配置文件路径 路径"/SiteConfig.xml"
/// </summary>
private static string _FilePath = "\\SiteConfig.Config";
public static string FilePath { get { return _FilePath; } set { _FilePath = value; } }

/// <summary>
/// 根节点对象
/// </summary>
public static xmlroot xmlroot()
{
XmlSerializer xs = new XmlSerializer(typeof(xmlroot));
Stream stream;
string strpath = "SiteConfig.xml";
try
{

stream = new FileStream(strpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return (xmlroot)xs.Deserialize(stream);
}
catch
{
return FormatConfig();
}
}

public static xmlroot FormatConfig()
{
xmlroot RT = new xmlroot();
try
{
XmlSerializer xs = new XmlSerializer(typeof(xmlroot));
Stream stream = new FileStream("SiteConfig.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
xs.Serialize(stream, RT);
stream.Close();
return RT;
}
catch
{
return null;
}

}
}

/// <summary>
/// 序列化对象类,根节点root
/// </summary>
[XmlRoot("root")]
public class xmlroot
{
public List<template> shoptemplate;
public List<template> newstemplate;

}
/// <summary>
/// 序列化对象类,子节点 root》template
/// </summary>
[Serializable]
public class template {
private string _text;
[XmlAttribute("text")]
public string text { get { return _text; } set { _text = value; } }

private string _space;
[XmlAttribute("space")]
public string space { get { return _space; } set { _space = value; } }

private string _class;
[XmlAttribute("class")]
public string Class { get { return _class; } set { _class = value; } }


private List<channel> _channel;
[XmlElement(ElementName = "channel")]
public List<channel> channel { get { return _channel; } set { _channel = value; } }

}

/// <summary>
/// 序列化对象类,子节点 root》template》channel
/// </summary>
[Serializable]
public class channel {
private string _text;
[XmlAttribute("text")]
public string text { get { return _text; } set { _text = value; } }

}



}




我试过站点下可以获取到节点

现在我改成 winform下 就出现问题
到

try
{

stream = new FileStream(strpath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
return (xmlroot)xs.Deserialize(stream);
}
catch
{
return FormatConfig();
}


XML 文档(2, 2)中有错误。

序列得到
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
...全文

相关推荐

java序列化和反序列化,面试必备

最近阅读Serializable接口和Externalizable接口的源码,并结合了一些资料,对面试过程中与序列化相关的内容做了一些总结。 一、序列化、反序列化、使用场景、意义。 序列化:将对象写入IO流中; 反序列化:从IO流中恢复对象; 意义:序列化机制允许将实现序列化的Java对象转换为字节序列,并将字节序列保存在磁盘中,或通过网络传输,以达到以后恢复成原来的对象。序列化机制使地对象可以脱离程序的运行而独立存在。 使用场景:所有在网络上传输的对象都必须是可序列化的。如:RMI (远程方法调用),传入的参数或返回的对象都是可序列化的,否则会出错。所有必须保存到磁盘的java对象都必须是可

weixin_38600460

c#序列化和反序列化

什么叫序列化? 我们都知道对象是暂时保存在内存中的,不能用U盘考走了,有时为了使用介质转移对象,并且把对象的状态保持下来,就需要把对象保存下来,这个过程就叫做序列化,通俗点,就是把人的魂(对象)收伏成一个石子(可传输的介质) 什么叫反序列化? 就是再把介质中的东西还原成对象,把石子还原成人的过程。 在进行这些操作的时候都需要这个可以被序列化,要能被序列化,就得给类头加[Serializable]特性。 通常网络程序为了传输安全才这么做。不知道回答清晰满意否?

木木文

protobuf常用序列化和反序列化API

protobuf常用序列化和反序列化API,相关教程:http://blog.csdn.net/tennysonsky/article/details/73920767

Mike江

Jedis序列化和反序列化使用jar

使用jedis操作redis技术使用,存储时候若是使用对象要对其进行序列化和反序列化。

god_mycode

JAVA序列化和反序列化的底层实现原理解析

主要介绍了JAVA序列化和反序列化的底层实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

weixin_38712874
评论 5
收藏 已收藏
分享
返回顶部
后浪
.NET社区 62,272 成员 668,968 内容