Deserialize XML data into object
来源:互联网 发布:js设置option选中 编辑:程序博客网 时间:2024/06/11 05:59
由于工作的需要,自己写了一个exmaple on 反序列 xml data 成 自定义的一个object:
主体:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
namespace NextLabs.RemindwithEmail
{
class Program
{
static void Main(string[] args)
{
emailcollection emails = null;
string path = Directory.GetCurrentDirectory().ToString() + @"\" + "EmailInformation.xml";
XmlSerializer serializer = new XmlSerializer(typeof(emailcollection));
StreamReader reader = new StreamReader(path);
emails = (emailcollection)serializer.Deserialize(reader);
reader.Close();
foreach (email myemail in emails)
{
Console.WriteLine(myemail.smtp);
Console.WriteLine(myemail.to);
Console.WriteLine(myemail.port);
Console.WriteLine(myemail.subject);
}
}
}
}
辅助:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Collections;
namespace NextLabs.RemindwithEmail
{
[Serializable]
public class email
{
[System.Xml.Serialization.XmlElement("smtp")]
public string smtp { get; set; }
[System.Xml.Serialization.XmlElement("port")]
public string port { get; set; }
[System.Xml.Serialization.XmlElement("to")]
public string to { get; set; }
[System.Xml.Serialization.XmlElement("subject")]
public string subject { get; set; }
}
[Serializable]
[System.Xml.Serialization.XmlRoot("emailcollection")]
public class emailcollection : IEnumerable
{
[System.Xml.Serialization.XmlArray("emailcollection")]
[System.Xml.Serialization.XmlArrayItem("email",typeof(email))]
public List<email> emails
{
get
{
return _emails;
}
set
{
_emails = value;
}
}
private List<email> _emails;
public void Add(object o)
{
if (_emails == null)
_emails = new List<email>();
_emails.Add(o as email);
}
public emailEnum GetEnumerator()
{
return new emailEnum(emails);
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
}
}
public class emailEnum : IEnumerator
{
public List<email> _emailcollection;
int postion = -1;
public emailEnum(List<email> list)
{
_emailcollection = list;
}
public void Reset()
{
postion = -1;
}
public bool MoveNext()
{
postion++;
return (postion < _emailcollection.Count);
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public email Current
{
get
{
try
{
return _emailcollection[postion];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
在这里我遇到几个问题:
1. 反序列时候 XmlArray("emailcollection") 对应的变量不能被定义为 List, 只能定义为 array。 其实是错的。 可以被定义为 list。
[System.Xml.Serialization.XmlArray("emailcollection")]
[System.Xml.Serialization.XmlArrayItem("email",typeof(email))]
public List<email> emails
2. 自定义的collection 要能使用foreach statoment,必须实现Getenumerator() 方法
微软解释
An IEnumerator object that can be used to iterate through the collection.
The foreach statement of the C# language (For Each in Visual Basic) hides the complexity of the enumerators. Therefore, using foreach is recommended, instead of directly manipulating the enumerator.
Enumerators can be used to read the data in the collection, but they cannot be used to modify the underlying collection.
Initially, the enumerator is positioned before the first element in the collection. The Reset method also brings the enumerator back to this position. At this position, the Current property is undefined. Therefore, you must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current.
Current returns the same object until either MoveNext or Reset is called. MoveNext sets Current to the next element.
If MoveNext passes the end of the collection, the enumerator is positioned after the last element in the collection and MoveNext returns false. When the enumerator is at this position, subsequent calls to MoveNext also return false. If the last call to MoveNext returns false, Current is undefined. To set Current to the first element of the collection again, you can callReset followed by MoveNext.
An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and its behavior is undefined.
The enumerator does not have exclusive access to the collection; therefore, enumerating through a collection is intrinsically not a thread-safe procedure. To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.
3. 反序列后,object 需要被添加。 添加时需要判定当前collection的状态,然后实习add,否则会进入deadlock然后得到stackoverexception
public void Add(object o)
{
if (_emails == null)
_emails = new List<email>();
_emails.Add(o as email);
}
- Deserialize XML data into object
- XmlNode Deserialize to object
- How to import xml data into excel
- transfer xml data into dataset and bind into gridview
- Unable to deserialize into session
- Store an Image(OLE Object) into Access Data Base
- XmlSerializer. Deserialize解析xml
- java xml Serialize DeSerialize
- XML 转化成Data Object(code 修改)
- tomcat+redis,Unable to deserialize into session
- Google Gson - deserialize list<class> object
- Encrypt/decrypt when serialize/deserialize data
- insert into data.mdb
- insert into data.mdb
- Embedding data into JPEG
- Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model
- Load XML matched Special Schema by Deserialize
- C# Serialize and Deserialize XML string
- 字节、字、bit、byte的关系
- MonetDB的SQL手册——Explain(执行计划)
- 范数
- 黑马程序员_Java基础语法1
- Android之PopupWindow弹出对话框
- Deserialize XML data into object
- hdu 1828 Picture(扫描线)
- 关于Nginx session sticky
- Xcode中禁用单个类文件不使用ARC
- Documentation/devicetree/bindings/xilinx.txt
- 洗牌算法汇总以及测试洗牌程序的正确性
- VC 调用C#写的COM组件注意事项
- cocos2d-x2.2.2(2.1.4及以上版本) VS2012 创建跨平台项目秒杀方法
- linux 命令备份