1 public static class PriceXML
2 {
3 ///<summary>
4 /// 新建一个XML文件
5 ///</summary>
6 ///<param name="list"></param>
7 ///<param name="description"></param>
8 ///<returns></returns>
9 public static bool AddPriceXML(List<B_Goods> list,string description)
10 {
11 if (list != null)
12 {
13 if (list.Count > 0)
14 {
15 //先创建XML,返回路径
16 XmlDocument xmldoc = new XmlDocument();
17 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
18 XmlDeclaration xmldecl;
19 xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
20 xmldoc.AppendChild(xmldecl);
21 //加入一个根元素
22 XmlNode xmlelem = xmldoc.CreateElement("", "PriceMark", "");
23 xmldoc.AppendChild(xmlelem);
24 XmlNode root = xmldoc.SelectSingleNode("PriceMark");//查找<PriceMark>
25 XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点
26 xe1.SetAttribute("name", description);//设置该节点name属性
27 foreach (var item in list)
28 {
29 XmlElement xesub1 = xmldoc.CreateElement("item");
30 xesub1.SetAttribute("pcode", item.G_AllProductID);
31 xesub1.SetAttribute("marketprice",item.G_MarketPrice.ToString());
32 xesub1.SetAttribute("sellprice", item.G_SellPrice.ToString());
33 if(item.G_IconDisplay==null)
34 xesub1.SetAttribute("icondisplay","");
35 else
36 xesub1.SetAttribute("icondisplay", item.G_IconDisplay);
37 xe1.AppendChild(xesub1);
38 }
39 root.AppendChild(xe1);//添加到<PriceMark>节点中
40 //然后在保存到源位置
41 xmldoc.AppendChild(xmlelem);
42 //保存创建好的XML文档
43 string filename = DateTime.Now.ToString("yyMMddHHmmss") + description + ".xml";
44 string path = System.Web.HttpContext.Current.Server.MapPath("/XMLDB/PriceTemp/") + filename;
45 xmldoc.Save(path);
46 return true;
47 }
48 }
49 return false;
50 }
51 //读取指定路径的PirceXML文件
52 public static List<B_Goods> ReadXML(string path)
53 {
54 List<B_Goods> listgood = new List<B_Goods>();
55 if (File.Exists(path))
56 {
57 XmlDocument xmlDoc = new XmlDocument();
58 xmlDoc.Load(path);
59 XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode("PriceMark");//获取PriceMark节点
60 if (xe.HasChildNodes)
61 {
62 XmlNodeList nodeList =xe.ChildNodes;//获取PriceMark节点的所有子节点
63 foreach (XmlNode item in nodeList)
64 {
65 B_Goods model = new B_Goods();
66 XmlElement xn=(XmlElement)item;
67 model.G_AllProductID = xn.GetAttribute("pcode");
68 model.G_MarketPrice = Convert.ToDecimal(xn.GetAttribute("marketprice"));
69 model.G_SellPrice =Convert.ToDecimal(xn.GetAttribute("sellprice"));
70 model.G_IconDisplay = xn.GetAttribute("icondisplay");
71 listgood.Add(model);
72 }
73
74 }
75 }
76 return listgood;
77 }
78 ///<summary>
79 /// 删除XML
80 ///</summary>
81 ///<param name="path"></param>
82 ///<returns></returns>
83 public static bool DeleteXML(string path)
84 {
85 if (File.Exists(path))
86 {
87 if (!IsUsed(path))
88 {
89 File.Delete(path);
90 return true;
91 }
92 else
93 return false;
94 }
95 return false;
96 }
97 ///<summary>
98 /// 判断文件是否被占用.lunx
99 ///</summary>
100 ///<param name="fileName"></param>
101 ///<returns></returns>
102 public static bool IsUsed(String fileName)
103 {
104 bool result = false;
105 try
106 {
107 FileStream fs = File.OpenWrite(fileName);
108 fs.Close();
109 }
110 catch
111 {
112 result = true;
113 }
114 return result;
115 }
116
117 ///<summary>
118 /// 创建XML
119 ///</summary>
120 ///<param name="description"></param>
121 ///<returns></returns>
122 public static string CreateXML(string description)
123 {
124 XmlDocument xmldoc = new XmlDocument();
125 //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
126 XmlDeclaration xmldecl;
127 xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null);
128 xmldoc.AppendChild(xmldecl);
129 //加入一个根元素
130 XmlNode xmlelem = xmldoc.CreateElement("", "PriceMark", "");
131 xmldoc.AppendChild(xmlelem);
132 //保存创建好的XML文档
133 string filename=DateTime.Now.ToString("yyMMddHHmmss")+description+".xml";
134 string path = System.Web.HttpContext.Current.Server.MapPath("/XMLDB/PriceTemp/") + filename;
135 xmldoc.Save(path);
136 return path;
137 }
138
139 }