using System; using System.Data; using System.Configuration; 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; using System.Collections.Generic;namespace NetView.Models {
/// <summary>
/// 加载常用网址信息
/// </summary>
protected void LoadWebDetail()
{
string filename = ImagesPath + "Xml/AdminLink.xml";
xmlDoc.Load(filename);
XmlNode node = xmlDoc.SelectSingleNode("AdminLink");
foreach (XmlNode node2 in node.ChildNodes)
{
if (node2.SelectSingleNode("id").InnerText == ViewState["id"].ToString())
{
txtname.Text = node2.SelectSingleNode("name").InnerText;
txturl.Text = node2.SelectSingleNode("url").InnerText;
break;
}
}
}
能简单点不?只要读一句就行了我第二天看C#,所以什么都不懂。
补充:工程是智能编程工程。在winform里
xmldoc.Load(路径);
XmlNode mainnode = xmldoc.SelectSingleNode("websdit ");
string s = mainnode.InnerText;//www.google.com.hk
是这样么
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("你项目根目录下xml文件名"));
string sReadXml=ds.tables[i].rows[j][k].toString();
//读出来的是个数据集,里面按照xml的节点生成表,因为不知道你的xml结构,所以ijk得你自己填
是windows mobile开发,好像没有Server
XML就一句:
<?xml version="1.0" encoding="utf-8" ?>
<websdit> http://wap.wdhn.net </websdit>
public static string GetConfig()
{
string elementName = null;
string workPath = Assembly.GetExecutingAssembly().GetName().CodeBase;
string Local = "";
workPath = workPath.Replace("AA.exe", "AAA.xml");
FileStream fs = new FileStream(workPath, FileMode.Open);
XmlTextReader xml = new XmlTextReader(fs);
xml.WhitespaceHandling = WhitespaceHandling.Significant;
while (xml.Read())
{
if (xml.NodeType == XmlNodeType.Element)
{
elementName = xml.Name;
}
else if (xml.NodeType == XmlNodeType.Text)
{
switch (elementName)
{
case "Local":
Local = xml.Value;
break;
default:
break;
}
}
}
xml.Close();
fs.Close();
return Local;
}
谢谢!~~~~
能简单点不?。。就一句:
<?xml version="1.0" encoding="utf-8" ?>
<websdit> http://wap.wdhn.net </websdit>
XML文件就在工程根目录下,config.xml。非常谢谢~~~
xmlDoc.Load(Server.MapPath("~/123.xml")); XmlNode Node = xmlDoc["websdit"];
string xxx= Node.InnerText;
够简单了吧。。
xmldoc.Load(AppDomain.CurrentDomain.BaseDirectory+“config.xml”);
XmlNode mainnode = xmldoc.SelectSingleNode("websdit ");
string s = mainnode.InnerText;//www.google.com.hk AppDomain.CurrentDomain.BaseDirectory就代表根目录,bin-》dubug文件夹下
是这个意思么?
呃不是。。开始可能没说清楚,不好意思。config.xml就是在建好的工程的工程文件夹下。和源文件是在同一个目录下。
谢谢~~~ 够简单但是还是没解决问题,是在w智能设备编程工程下。没有Server
using System;
using System.Data;
using System.Configuration;
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;
using System.Collections.Generic;namespace NetView.Models
{
/// <summary>
/// XmlHelper 的摘要说明
/// </summary>
public class XmlHelper
{
private string msg;
public XmlHelper()
{
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/**************************************************
* 使用示列:
* XmlHelper.Read(path, "/Node", "")
* XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
************************************************/
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
} // public static void Insert(string path, string node, string node1, string element, string attribute, string value, string attribute1,string value1)
//{
// XmlDocument xmlDoc = new XmlDocument();
// xmlDoc.Load(path); // XmlNode root = xmlDoc.SelectSingleNode(node);//查找
// XmlElement xe1 = xmlDoc.CreateElement(node1);//创建一个节点
// // xe1.SetAttribute("name", "InsertNode");//设置该节点属性
// Dictionary<string, string> dictionary = new Dictionary<string, string>();
// dictionary.Add("Money", "2000");
// dictionary.Add("Title", "这是后加入的一个结点");
// //插入结点信息
// foreach (KeyValuePair<string, string> kvp in dictionary)
// {
// XmlElement xesub1 = xmlDoc.CreateElement(element);
// xesub1.SetAttribute(attribute, value);
// xesub1.SetAttribute(attribute1, value1);
// xe1.AppendChild(xesub1);
// } // //添加到节点中
// root.AppendChild(xe1);
// xmlDoc.Save(path);
// //try
// //{
// // XmlDocument doc = new XmlDocument();
// // doc.Load(path);
// // XmlNode xn =doc.SelectSingleNode(node); //查找node节点
// // if(xn!=null)
// // {
// // XmlNode xel = doc.SelectSingleNode(node1);
// // if (xel != null)
// // //if (element.Equals(""))
// // //{
// // // if (!attribute.Equals(""))
// // // {
// // // XmlElement xe = (XmlElement)xe1;
// // // xe.SetAttribute(attribute, value);
// // // }
// // //}
// // //else
// // //{
// // {
// // XmlElement xe = doc.CreateElement(element);
// // if (attribute.Equals(""))
// // xe.InnerText = value;
// // else
// // {
// // xe.SetAttribute(attribute, value);
// // xe.SetAttribute(attribute1, value1);
// // xel.AppendChild(xe);
// // xn.AppendChild(xel);
// // }
// // //}
// // doc.Save(path);
// // }
// // } // //}
// //catch { }
//}
/// <summary>
/// 插入
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="node1"></param>
/// <param name="element"></param>
/// <param name="attribute"></param>
/// <param name="value"></param>
public static void Insert(string path, string node, string node1, string element, string attribute, string value, string attribute1, string value1)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node); //查找根node节点
XmlElement xel = doc.CreateElement(node1);
//if (element.Equals(""))
//{
// if (!attribute.Equals(""))
// {
// XmlElement xe = (XmlElement)xe1;
// xe.SetAttribute(attribute, value);
// }
//}
//else
//{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xe.SetAttribute(attribute1, value1);
xel.AppendChild(xe);
xn.AppendChild(xel);
//}
doc.Save(path); }
catch { }
}
/// <summary>
/// 插入数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
/// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path, "/Node", "Element", "", "Value")
* XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
* XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
************************************************/
public static void Insert(string path, string node, string element, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
/// 修改数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Insert(path, "/Node", "", "Value")
* XmlHelper.Insert(path, "/Node", "Attribute", "Value")
************************************************/
public static void Update(string path, string node, string attribute, string value)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
XmlElement xe = (XmlElement)xn;
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
doc.Save(path);
}
catch { }
} /// <summary>
/// 删除数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
/// <param name="value">值</param>
/// <returns></returns>
/**************************************************
* 使用示列:
* XmlHelper.Delete(path, "/Node", "")
* XmlHelper.Delete(path, "/Node", "Attribute")
************************************************/
public static void Delete(string path, string node, string attribute)
{
// XmlDocument doc = new XmlDocument();
// doc.Load(path);
// XmlNode xn = doc.SelectSingleNode(node);
// string mainNode = node.Substring(0, node.LastIndexOf("/"));
//doc.SelectSingleNode(mainNode).RemoveChild(doc.SelectSingleNode(node)); try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
// XmlNode xn = doc.SelectSingleNode(node);
XmlNodeList xnl = doc.SelectSingleNode(node).ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlNodeList xnl1 = xn.SelectSingleNode("RedirectUrl").ChildNodes;
XmlElement xe = (XmlElement)xn;
for (int j = 0; j < xnl1.Count; j++)
{
XmlElement xe1 = (XmlElement)xnl1.Item(j);
if (xe1.GetAttribute("UrlName") == "小路工作室1231231231")
{
xn.RemoveChild(xe1);
if (j < xnl1.Count) j = j - 1;
}
}
// if (xe.GetAttribute("genre") == "fantasy")
//{
// xe.RemoveAttribute("genre");//删除genre属性
// }
// else if (xe.GetAttribute("genre") == "update李赞红")
//{
// xe.RemoveAll();//删除该节点的全部内容
// }
} //for (int i = 0; i < xnl.Count; i++)
//{
// XmlNodeList xnl1 = xnl.Item(i).SelectSingleNode("RedirectUrl").ChildNodes;
// for (int j = 0; j < xnl1.Count; j++)
// {
// XmlElement xe = (XmlElement)xnl1.Item(j);
// if (xe.GetAttribute("UrlName") == "小路工作室1231231231")
// {
// xn.RemoveChild(xe);
// if (i < xnl1.Count) i = i - 1;
// }
// }
//} //XmlElement xe = (XmlElement)xn;
//if (attribute.Equals(""))
// xn.ParentNode.RemoveChild(xn);
//else
// xe.RemoveAttribute(attribute);
doc.Save(path);
}
catch { }
} /// <summary>
/// 删除属性带key的节点,key:GUID
/// </summary>
/// <param name="path">XML文件路径</param>
/// <param name="rootnode">根节点</param>
/// <param name="key">key:Guid</param>
/// <param name="msg">返回的信息</param>
public static void DeleteXMLNode(string path, string rootnode, string znode,string type,string key)
{
try
{
if (System.IO.File.Exists(path))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList xl = xmlDoc.SelectSingleNode(rootnode).ChildNodes;
for (int i = 0; i < xl.Count; i++)
{
XmlElement xe = (XmlElement)xl[i];//第i个dbGust子节点
XmlNodeList node = xe.GetElementsByTagName(znode);
if (node.Count > 0)
{
for (int j = 0; j < node.Count; j++)
{
XmlNode node1 = node.Item(j);
XmlElement xe1 = (XmlElement)node.Item(j);
if (xe1.GetAttribute(type) == key)
{
xmlDoc.SelectSingleNode(rootnode).RemoveChild(node[0].ParentNode);//删除该节点
break;
}
}
}
}
xmlDoc.Save(path);
}
}
catch (XmlException ex)
{ } }
/// <summary>
/// 返回符合指定名称的节点数
/// </summary>
/// <param name="nodeName">节点名</param>
/// <returns>节点数</returns>
public static int Count(string path,string nodeName,string znode)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
XmlNodeList nodeList = xe.GetElementsByTagName(znode);
return nodeList.Count;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
/// <summary>
/// 返回最后的数字
/// </summary>
/// <param name="path"></param>
/// <param name="nodeName"></param>
/// <param name="znode"></param>
/// <returns></returns>
public static string num(string path, string nodeName, string znode,string type)
{
try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlElement xe = (XmlElement)xmlDoc.SelectSingleNode(nodeName); //查找根node节点
XmlNodeList nodeList = xe.GetElementsByTagName(znode);
int i = nodeList.Count;
XmlElement xe1 = (XmlElement)nodeList.Item(nodeList.Count-1);
string inum=xe1.GetAttribute(type);
return inum;
}
catch (Exception e)
{
throw (new Exception(e.Message));
}
}
}
}
MyXML.Load("1.xml");//自己改文件名
XmlNode Node = MyXML.DocumentElement;
XmlNodeList nodes;
XmlElement root = MyXML.DocumentElement;
nodes = root.SelectNodes("//websdit");
你如果实在偷懒, 自己按字符读, 也可以读出来吗.
//<-- + zbf 2010/08/11
/// <summary>
/// 获取XML某个节点的属性值 如 <attProvider defaultProvider="SqlAttProvider"></attProvider>中的defaultProvider的值
/// </summary>
/// <param name="xPath">查询到该节点的连接</param>
/// <param name="keyName">属性名 defaultProvider</param>
/// <returns></returns>
public static string GetConfigKeyValue(string xPath, string keyName)
{
string result = string.Empty;
try
{
XmlDocument xmlDoc = new XmlDocument();
string xlmPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
xmlDoc.Load(xlmPath);//读入xlm配置文档
XmlNodeList nodeList = xmlDoc.SelectSingleNode(xPath).ChildNodes;
if (nodeList.Count == 1)//只处理只有一个主属性分支的情况
{
XmlElement xe = (XmlElement)nodeList[0];
result = xe.GetAttribute(keyName);
}
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
// <providers>
// <add name="AccessAttProvider" type="RAS.Providers.AccessAttProvider,RAS"/>
// <add name="SqlAttProvider" connectionStringName="gLocalSqlServer" type="RAS.Providers.SqlAttProvider,RAS"/>
//</providers>
/// <summary>
/// 获取节点下面某个属性支的某个属性值 如获取<add name="AccessAttProvider" type="RAS.Providers.AccessAttProvider,RAS"/>的type值
/// </summary>
/// <param name="xPath">查询路径</param>
/// <param name="unicodKeyName">第一个属性名</param>
/// <param name="keyValue">第一个属性名的值</param>
/// <param name="key">要查询的属性key</param>
/// <returns></returns>
public static string GetConfigKeyValue(string xPath, string unicodKeyName, string keyValue, string key)
{
string result = string.Empty;
try
{
XmlDocument xmlDoc = new XmlDocument();
string xlmPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
xmlDoc.Load(xlmPath);//读入xlm配置文档
XmlNodeList nodeList = xmlDoc.SelectSingleNode(xPath).ChildNodes; foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute(unicodKeyName).IndexOf(keyValue) != -1)//找到主属性支
{
result = xe.GetAttribute(key);
break;
}
}
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
return result;
} /// <summary>
/// 设置XML某个节点的属性值 如 <attProvider defaultProvider="SqlAttProvider"></attProvider>中的defaultProvider的值
/// </summary>
/// <param name="xPath"></param>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool SetConfigKeyValue(string xPath, string keyName, string keyValue)
{
bool result = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string xlmPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
xmlDoc.Load(xlmPath);//读入xlm配置文档
XmlNodeList nodeList = xmlDoc.SelectSingleNode(xPath).ChildNodes;
if (nodeList.Count == 1)//只处理只有一个主属性分支的情况
{
XmlElement xe = (XmlElement)nodeList[0];
xe.SetAttribute(keyName, keyValue);
xmlDoc.Save(xlmPath);
result = true;
}
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
/// <summary>
/// 设置节点下面某个属性支的某个属性值 如<add name="AccessAttProvider" type="RAS.Providers.AccessAttProvider,RAS"/>的type值
/// </summary>
/// <param name="xPath"></param>
/// <param name="unicodKeyName"></param>
/// <param name="unicodKeyValue"></param>
/// <param name="key"></param>
/// <param name="keyValue"></param>
/// <returns></returns>
public static bool SetConfigKeyValue(string xPath, string unicodKeyName, string unicodKeyValue, string keyName, string keyValue)
{
bool result = false;
try
{
XmlDocument xmlDoc = new XmlDocument();
string xlmPath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
xmlDoc.Load(xlmPath);//读入xlm配置文档
XmlNodeList nodeList = xmlDoc.SelectSingleNode(xPath).ChildNodes; foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute(unicodKeyName).IndexOf(unicodKeyValue) != -1)//找到主属性支
{
xe.SetAttribute(keyName, keyValue);
xmlDoc.Save(xlmPath);
result = true;
break;
}
}
}
catch (System.NullReferenceException NullEx)
{
throw NullEx;
}
catch (Exception ex)
{
throw ex;
}
return result;
}
晕! 我哪有重复粘贴了! 那是一个对xml操作类!
路径的话建议写在配置文件里用ConfigurationManager读取,这样最灵活