易截截图软件、单文件、免安装、纯绿色、仅160KB

C#序列化xml的完整例子

C#序列化xml
关键步骤:
XmlSerializer xSerializer = new XmlSerializer(typeof(MyObj));
StringWriter sWriter = new StringWriter(CultureInfo.InvariantCulture);
XmlTextWriter xTextWriter = new XmlTextWriter(sWriter);
XmlDocument xmlDoc = new XmlDocument();
xSerializer.Serialize(xTextWriter, myObj);
xmlDoc.LoadXml(sWriter.ToString()); 
需要说明的是,如果要序列化的object所在的类里有带参数的构造函数,还必须添加一个无参数的构造函数,否则,会报object cannot be serialized because it does not have a parameterless constructor的错误。下面是一个完整的实例,仅供参考。
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
/* The XmlRootAttribute allows you to set an alternate name
(PurchaseOrder) of the XML element, the element namespace; by
default, the XmlSerializer uses the class name. The attribute
also allows you to set the XML namespace for the element. Lastly,
the attribute sets the IsNullable property, which specifies whether
the xsi:null attribute appears if the class instance is set to
a null reference. */
[XmlRootAttribute("PurchaseOrder", Namespace="http://www.cpandl.com",
IsNullable = false)]
public class PurchaseOrder
{
public Address ShipTo;
public string OrderDate;
/* The XmlArrayAttribute changes the XML element name
from the default of "OrderedItems" to "Items". */
[XmlArrayAttribute("Items")]
public OrderedItem[] OrderedItems;
public decimal SubTotal;
public decimal ShipCost;
public decimal TotalCost;
}
public class Address
{
/* The XmlAttribute instructs the XmlSerializer to serialize the Name
field as an XML attribute instead of an XML element (the default
behavior). */
[XmlAttribute]
public string Name;
public string Line1;
/* Setting the IsNull


相关文档:

从xml读取游戏配置信息或保存

 /*
* 主要作用;
* 从xml读取游戏配置信息或保存
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Reflection;
namespace Game
{
class Config
{
Ke ......

Delphi与C#之父:技术理想架构开发传奇

    Anders Hejlsberg(安德斯-海森博格) 坐在自己的办公室,双眼直直的盯着前方。他要做一个决定,决定自己未来的命运和理想。这是1996年一个普通的下午,几个小时前,他刚与比尔-盖茨结束了一次愉快的午餐,同为软件界的精英,他们相谈甚欢,盖茨给他开出三百万以上的年薪和数万股的微软股票。这是一个诱人 ......

XMLNameSpace XML命名空间

XML 命名空间提供了一种避免元素命名冲突的方法。
命名空间属性一般放置在元素的开始标记处,其使用语法如下所示:
xmlns:namespace-prefix="namespace",
例如:xmlns:f="http://www.w3schools.com/furniture"
W3C 命名规范声明命名空间本身就是一个统一资源标示符,Uniform Resource Identifier (URI)。
asp.net读取X ......

C#.NET防止SQL注入式攻击

1  防止sql注入式攻击(可用于UI层控制) #region  防止sql注入式攻击(可用于UI层控制)
  2 
  3   /**/ /// 
  4  ///  判断字符串中是否有SQL攻击代码
  5  /// 
  6  ///  传入用户提交数据
  7  ///  ......

用python, c#, java写的文件拷贝

     前一段时间试着用这三种语言简单的写了关于文件拷贝的程序,发现c#和python的api惊人的相似,对于文件的操作这两种语言非常的方便。都没有加异常的处理
C#源代码:
        public static void CopyFile(string source, string destination)
  & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号