发布网友 发布时间:2024-10-23 02:43
共4个回答
热心网友 时间:2024-11-04 11:29
你好!
public string Get_Xml(string xml_name)
{
string xmlpath = Server.MapPath("~") + "/SystemConfig.xml";//这里是路径,其实这个也可以作为参数
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlpath);
XmlNodeList nodelist = xmldoc.SelectSingleNode("setup").ChildNodes;//第一个节点
string rusult=nodelist.Item(0).SelectSingleNode(xml_name).InnerText;
return rusult;
}
这个方法,只适合你给出的这种格式的XML,如果想同样其他的,就需要遍历判断等等的!
热心网友 时间:2024-11-04 11:29
public class XmlReader
{
public XmlReader(string XmlFileName,string NodeName)
{
XmlDocument Doc=new XmlDocument();
Doc.Load(XmlFileName);
XmlNode xn=Doc .SelectSingleNode("setup/ConnectionString");
this.Value =xn.SelectSingleNode(NodeName).InnerText;
}
public string Value;
}
/*
-------------用法如下----------------------
private void button1_Click(object sender, EventArgs e)
{
XmlReader xr =new XmlReader ("改成你的xml文件名.xml","server");
string server = xr.Value;
MessageBox.Show(server);
}
*/
热心网友 时间:2024-11-04 11:30
class Program
{
static void Main(string[] args)
{
Console.WriteLine(XmlParser.GetValue("server"));
}
}
public class XmlParser
{
public static string GetValue(string name)
{
string content = @"
<setup>
<ConnectionString>
<server>192.168.0.21</server>
<uid>sa</uid>
<pwd>123456</pwd>
<datebase>aaaaaaa</datebase>
<timeout>5</timeout>
<jiancewangluo>0</jiancewangluo>
</ConnectionString>
</setup>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(content);
XmlNode conn = doc.SelectSingleNode("setup/ConnectionString");
return conn.SelectSingleNode(name).InnerText;
}
}
热心网友 时间:2024-11-04 11:30
private void ReadXml()
{
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load("xml文件名");
XmlElement Root = XmlDoc.SelectSingleNode("setup");
XmlNodeList AllXN1 = Root.ChildNodes;
foreach (XmlNode xn1 in AllXN1) {
XmlNodeList AllXN2 = xn1.ChildNodes;
foreach (XmlNode xn in AllXN2) {
switch (xn.Name) {
case "server":
想要的值=xn.InnerText;
break:
}
}
}
}