Loading...
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/// <summary>
        /// XmlHelper 的摘要说明。
        /// xml操作类
        /// </summary>
        public class XmlHelper
        {
            protected string strXmlFile;
            protected XmlDocument objXmlDoc = new XmlDocument();
 
            public XmlHelper(string XmlFile)
            {
                //
                // TODO: 在这里加入建构函式的程序代码
                //
                try
                {
 
                    objXmlDoc.Load(XmlFile);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                strXmlFile = XmlFile;
            }
 
            public DataTable  GetData(string XmlPathNode)
            {
                //查找数据。返回一个DataView
                DataSet ds = new DataSet();
                StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
                ds.ReadXml(read);
                return ds.Tables[0];
            }
            /// <summary>
            /// 新节点内容。
            /// 示例:xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
            /// </summary>
            /// <param name="XmlPathNode"></param>
            /// <param name="Content"></param>
            public void Replace(string XmlPathNode, string Content)
            {
                //更新节点内容。
                objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
            }
 
            /// <summary>
            /// 删除一个指定节点的子节点。
            /// 示例: xmlTool.DeleteChild("Book/Authors[ISBN=\"0003\"]");
            /// </summary>
            /// <param name="Node"></param>
            public void DeleteChild(string Node)
            {
                //删除一个节点。
                string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
                objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
            }
 
 
 
            /// <summary>
 
            ///  * 使用示列:
          ///  示例: XmlHelper.Delete( "/Node", "")
         ///  XmlHelper.Delete( "/Node", "Attribute")
           /// </summary>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
            public   void Delete(  string node, string attribute)
        {
            try
            {
 
                XmlNode xn = objXmlDoc.SelectSingleNode(node);
                XmlElement xe = (XmlElement)xn;
                if (attribute.Equals(""))
                    xn.ParentNode.RemoveChild(xn);
                else
                    xe.RemoveAttribute(attribute);
 
            }
            catch { }
        }
 
 
            /// <summary>
            /// 插入一节点和此节点的一子节点。
            /// 示例:xmlTool.InsertNode("Book","Author","ISBN","0004");
            /// </summary>
            /// <param name="MainNode">主节点</param>
            /// <param name="ChildNode">子节点</param>
            /// <param name="Element">元素</param>
            /// <param name="Content">内容</param>
            public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
            {
                //插入一节点和此节点的一子节点。
                XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
                objRootNode.AppendChild(objChildNode);
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objChildNode.AppendChild(objElement);
            }
 
            /// <summary>
            /// 插入一个节点,带一属性。
            /// 示例: xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
            /// </summary>
            /// <param name="MainNode">主节点</param>
            /// <param name="Element">元素</param>
            /// <param name="Attrib">属性</param>
            /// <param name="AttribContent">属性内容</param>
            /// <param name="Content">元素内容</param>
            public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
            {
                //插入一个节点,带一属性。
                XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.SetAttribute(Attrib, AttribContent);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }
            /// <summary>
            /// 插入一个节点,不带属性。
            /// 示例:xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
            /// </summary>
            /// <param name="MainNode">主节点</param>
            /// <param name="Element">元素</param>
            /// <param name="Content">元素内容</param>
            public void InsertElement(string MainNode, string Element, string Content)
            {
                //插入一个节点,不带属性。
                XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
                XmlElement objElement = objXmlDoc.CreateElement(Element);
                objElement.InnerText = Content;
                objNode.AppendChild(objElement);
            }
 
            /// <summary>
            /// 对xml文件做插入,更新,删除后需做Save()操作,以保存修改
            /// </summary>
            public void Save()
            {
                //保存文檔。
                try
                {
                    objXmlDoc.Save(strXmlFile);
                }
                catch (System.Exception ex)
                {
                    throw ex;
                }
                objXmlDoc = null;
            }
        }
 
        //=========================================================
 
        //实例应用:
 
        //string strXmlFile = Server.MapPath("TestXml.xml");
        //XmlControl xmlTool = new XmlControl(strXmlFile);
 
        // 数据显视
        // dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]");
        // dgList.DataBind();
 
        // 更新元素内容
        // xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
        // xmlTool.Save();
 
        // 添加一个新节点
        // xmlTool.InsertNode("Book","Author","ISBN","0004");
        // xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
        // xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
        // xmlTool.Save();
 
        // 删除一个指定节点的所有内容和属性
        // xmlTool.Delete("Book/Author[ISBN=\"0004\"]");
        // xmlTool.Save();
 
        // 删除一个指定节点的子节点
        // xmlTool.Delete("Book/Authors[ISBN=\"0003\"]");
        // xmlTool.Save();
版权所有©转载必须以链接形式注明作者和原始出处:江湖源码 » C#常用操作类库三(XML操作类)

: http://www.jhcms.net/index.php/2020/11/13/c%e5%b8%b8%e7%94%a8%e6%93%8d%e4%bd%9c%e7%b1%bb%e5%ba%93%e4%b8%89xml%e6%93%8d%e4%bd%9c%e7%b1%bb/

No comments yet.