1 获取XML文件的路径
protected
static
String ServerRoot()
... {
return HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).TrimEnd('/');
}
... {
return HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath).TrimEnd('/');
}
2 将XML中的所有节点用一定规则将所有的节点组装成一个String,可以在不同节点元素之间加入一个符号. 便于以后处理的时候用split()方法处理,放入一个数组里面,这样操作XML各个节点就变换成操作一个数组了。
private
static
String ShowXML(XmlNodeList nodeList)
... {
String strReturn = "";
foreach (XmlNode node in nodeList)
...{
if (node.NodeType != XmlNodeType.Comment)
...{
if (node.HasChildNodes)
...{
foreach (XmlNode nn in node.ChildNodes)
...{
strReturn += nn.InnerText;
strReturn += "*";
}
}
//strReturn = strReturn.Substring(0, strReturn.Length - 1);
//不同节点之间
strReturn += "?";
}
}
strReturn = strReturn.Substring(0, strReturn.Length - 1);
return strReturn;
}
... {
String strReturn = "";
foreach (XmlNode node in nodeList)
...{
if (node.NodeType != XmlNodeType.Comment)
...{
if (node.HasChildNodes)
...{
foreach (XmlNode nn in node.ChildNodes)
...{
strReturn += nn.InnerText;
strReturn += "*";
}
}
//strReturn = strReturn.Substring(0, strReturn.Length - 1);
//不同节点之间
strReturn += "?";
}
}
strReturn = strReturn.Substring(0, strReturn.Length - 1);
return strReturn;
}
3
//
指定XML文件路径,获得节点值
private static String GetByPath(String strPath, String strKey)
... {
XmlDocument xDoc = null;
String strReturn = "";
try
...{
xDoc = new XmlDocument();
xDoc.Load(strPath);
XmlElement xElem = (XmlElement)(xDoc.SelectSingleNode(strKey));
if (xElem != null)
...{
String str = xElem.InnerText.Replace(" ", "");
strReturn = str.Trim();
}
}
catch (Exception e)
...{
String str = e.Message;
strReturn = "";
}
finally
...{
xDoc = null;
}
return strReturn;
}
private static String GetByPath(String strPath, String strKey)
... {
XmlDocument xDoc = null;
String strReturn = "";
try
...{
xDoc = new XmlDocument();
xDoc.Load(strPath);
XmlElement xElem = (XmlElement)(xDoc.SelectSingleNode(strKey));
if (xElem != null)
...{
String str = xElem.InnerText.Replace(" ", "");
strReturn = str.Trim();
}
}
catch (Exception e)
...{
String str = e.Message;
strReturn = "";
}
finally
...{
xDoc = null;
}
return strReturn;
}
4
//
指定路径,获得指定节点及其所有子节点的值(暂时用于Style的读取)
private static String GetAllByPath(String strPath, String strKey)
... {
XmlDocument xDoc = null;
String strReturn = "";
try
...{
xDoc = new XmlDocument();
xDoc.Load(strPath);
XmlElement xElem = (XmlElement)(xDoc.SelectSingleNode(strKey));
if (xElem != null)
...{
XmlNodeList nodeList = xElem.ChildNodes;
strReturn = ShowXML(nodeList);
}
}
catch (Exception e)
...{
String str = e.Message;
}
return strReturn;
}
private static String GetAllByPath(String strPath, String strKey)
... {
XmlDocument xDoc = null;
String strReturn = "";
try
...{
xDoc = new XmlDocument();
xDoc.Load(strPath);
XmlElement xElem = (XmlElement)(xDoc.SelectSingleNode(strKey));
if (xElem != null)
...{
XmlNodeList nodeList = xElem.ChildNodes;
strReturn = ShowXML(nodeList);
}
}
catch (Exception e)
...{
String str = e.Message;
}
return strReturn;
}