110,529
社区成员
发帖
与我相关
我的任务
分享
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(@"<students>
<student pkid='111'>
<name>aaa</name>
<age>22</age>
</student>
<student pkid='222'>
<name>bbb</name>
<age>24</age>
</student>
</students>"
);
XmlNode node = xmlDoc.SelectSingleNode("students/student[@pkid='222']");
if (node != null)
{
Console.WriteLine(node.FirstChild.InnerText); //return bbb;
}
" <students>
<student pkid="111">
<name>aaa</name>
<age>22</age>
</student>
<student pkid="222">
<name>bbb</name>
<age>24</age>
</student>
</students>"
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class XML示例 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
public class XmlProvider : IDisposable
{
private string a;
private XmlDocument b;
public XmlProvider(string XmlFile)
{
try
{
this.b = new XmlDocument();
this.b.Load(XmlFile);
}
catch
{
}
this.a = XmlFile;
}
public void DeleteNode(string NodeName)
{
string xpath = NodeName.Substring(0,NodeName.LastIndexOf("/"));
this.b.SelectSingleNode(xpath).RemoveChild(this.b.SelectSingleNode(NodeName));
}
public void Dispose()
{
this.Dispose(true);
}
protected void Dispose(bool Diposing)
{
this.b = null;
}
public void InsetElement(string mainNode, string element, string content)
{
XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(element);
newChild.InnerText = content;
node.AppendChild(newChild);
}
public void InsertElement(string mainNode, string element, string attrib, string attribConte, string content)
{
XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(element);
newChild.SetAttribute(attrib, attribConte);
newChild.InnerText = content;
node.AppendChild(newChild);
}
public void InsertNode(string mainNode, string childNode, string element, string contents)
{
XmlNode node = this.b.SelectSingleNode(mainNode);
XmlElement newChild = this.b.CreateElement(childNode);
node.AppendChild(newChild);
XmlElement element3 = this.b.CreateElement(element);
element3.InnerText = contents;
newChild.AppendChild(element3);
}
public string ReadAtribute(string PathNode, string AttubuteName)
{
string str = "";
try
{
str = this.b.SelectSingleNode(PathNode).Attributes[AttubuteName].Value;
}
catch
{
}
return str;
}
public string ReadInnerText(string PathNode)
{
return this.b.SelectSingleNode(PathNode).InnerText;
}
public XmlNode ReadNode(string PathNode)
{
return this.b.SelectSingleNode(PathNode);
}
public void Save()
{
try
{
this.b.Save(this.a);
}
catch
{
}
this.b = null;
}
public void UpdateAttribute(string PathNode, string AttributeName, int AttributeValue)
{
this.b.SelectSingleNode(PathNode).Attributes[AttributeName].Value = AttributeValue;
}
public void UpdateInnerText(string PathNode, string InnerText)
{
this.b.SelectSingleNode(PathNode).InnerText = InnerText;
}
}