讀取xml檔案轉成List物件的兩種方法

2021-06-19 18:35:53 字數 880 閱讀 5057

xml格式如下:

<?xml version="1.0"?>

product 物件如下:

public class product

public decimal price

public decimal supplierid

}利用.net中的xmlserializer將xml轉換成物件

1、首先要在product、products類中的每個屬性上加上與xml對應的描述字段,如下**:

[xmlroot("

products")]

public

class

products

}

public

class

product

[xmlattribute(attributename = "

price")]

public

decimal price

[xmlattribute(attributename = "

supplierid")]

public

decimal supplierid

}

注意attributename一定要和xml中的一致。

2、相應的對應關係建立好了之後,下面就來進行讀取反序列化,**如下:

private static ilistproducts=new list();

static loadxml()}}

catch (exception ex)

}

**

讀取XML檔案

private void button2 click object sender,eventargs e using filestream fs new filestream filename,filemode.open,fileaccess.read n xl.name 是否有屬性 if xl.h...

xml檔案讀取

xml檔案讀取 利用庫tinyxml 示例xml 獲取對應路徑上的xml if document null tixmlelement nodeelement document rootelement 獲取根節點 tixmlelement objectelement nodeelement first...

xmlhelper (把實體物件轉成xml文件)

08 55 14 2012 03 19 public static class xmlhelper foreach propertyinfo propinfo in propinfos return sb.tostring endregion region 使用xml初始化實體類容器 使用xml初始...