易截截图软件、单文件、免安装、纯绿色、仅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


相关文档:

如何在VC环境下编写程序读取XML文件

如何在VC环境下编写程序读取XML文件?清提供源代码.谢谢.
#import <msxml3.dll> named_guids  //导入动态库,装了IE5就有
using namespace MSXML2;     //引用命名空间,一般可以把命名空间理解成类和接口的集合,呵呵,对不对我也不知道了
#include <vector>
using na ......

MD5加密的一般方法(ASP.NET / C#2005)

  /// <summary>
/// 方法一:通过使用 new 运算符创建对象
/// </summary>
/// <param name="strSource">需要加密的明文</param>
/// <returns>返回16位加密结果,该结果取32位加密结果的第9位到25位</returns>
public string Get_MD5_Method1(strin ......

XMLNameSpace XML命名空间

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

超级大笨狼中国象棋(js+xml)

<html xmlns:v="urn:schemas-microsoft-com:vml">
<head>
 <STYLE>
  v\:* { Behavior: url(#default#VML) }
  body {
   background-color:DarkGoldenrod;
   margin-left: 10px;
 &nbs ......

FLASH+XML不显示中文或无法显示中文的解决方法

常常在网上看到一些很的FLASH效果可是下下来后才发现中文不支持或都中文无法显示的问题,所以在网上找了很多资料终于找到了一个简单的方法来解决这个问题,在这里与大家分享一下。
点南嵌入按钮,按下ctrl选中大写,小写,数字,标点符号,中文(全部)后,确定!!
 这样就OK了。 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号