forked from xiaominglei001/MakeSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlManager.cs
142 lines (129 loc) · 4.31 KB
/
XmlManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace MakeSQL
{
public class XmlManager
{
/// <summary>
/// 创建xml
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="item">节点集</param>
/// <returns></returns>
public static int CreateXML(string filePath,string items)
{
try
{
XmlTextWriter writer = new XmlTextWriter(filePath, null);
//使用自动缩进便于阅读
writer.Formatting = Formatting.Indented;
//写入根元素
writer.WriteStartElement("items");
writer.WriteStartElement("item");
////写入属性及属性的名字
//writer.WriteAttributeString("类别", "文学");
//writer.WriteAttributeString("品质", "优");
string[] itemArray = items.Split(new char[] { ',' });
//加入子元素
for (int i = 0; i < itemArray.Length; i++)
{
if (string.IsNullOrEmpty(itemArray[i]))
{
continue;
}
writer.WriteElementString(itemArray[i], "");
//writer.WriteElementString("author", "");
//writer.WriteElementString("price", "");
}
//关闭根元素,并书写结束标签
writer.WriteEndElement();
writer.WriteEndElement();
//将XML写入文件并且关闭XmlTextWriter
writer.Close();
}
catch (Exception ex)
{
return -1;
}
return 1;
}
/// <summary>
/// 更新xml
/// </summary>
/// <param name="filePath">路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeValue">节点值</param>
/// <returns></returns>
public static int UpdateXML(string filePath, string nodeName, string nodeValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode xns = xmlDoc.SelectSingleNode("Items");
XmlNodeList xnl = xns.ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.Name == nodeName)
{
xe.InnerText = nodeValue;
break;
}
}
xmlDoc.Save(filePath);
}
catch (Exception ex)
{
return -1;
}
return 1;
}
/// <summary>
/// 读取节点值
/// </summary>
/// <param name="filePath">文件路径</param>
/// <param name="nodeName">节点名称</param>
/// <param name="nodeValue">节点值</param>
/// <returns></returns>
public static int ReadNodeValue(string filePath, string nodeName, ref string nodeValue)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode xns = xmlDoc.SelectSingleNode("Items");
XmlNodeList xnl = xns.ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (xe.Name == nodeName)
{
nodeValue = xe.InnerText;
break;
}
}
}
catch (Exception ex)
{
return -1;
}
return 1;
}
/// <summary>
/// 获取xml值
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public static string GetXmlString(string filePath)
{
string xml = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
xml = xmlDoc.InnerXml;
return xml;
}
}
}