/// <summary>
/// XmlDocument扩展类
///
/// 目的:用于优化和减少代码书写量
/// 备注:Element 译为:元素
/// Document 译为:文档
/// Node 译为:节点
/// </summary>
public class XmlDocumentExtender : XmlDocument
{
public XmlDocumentExtender()
: base()
{
}
#region 扩展的构造函数
#if NET1
public XmlDocumentExtender(XmlNameTable nt)
: base(new XmlImplementation())
{
}
#else
public XmlDocumentExtender(XmlNameTable nt)
: base(new XmlImplementation(nt))
{
}
#endif
#endregion
/// <summary>
/// 加载的文件名(含路径)
/// </summary>
/// <param name="filename"></param>
public override void Load(string filename)
{
if (MauniteCommonFileUtilsFileExists(filename))
{
baseLoad(filename);
}
else
{
throw new Exception("文件: " + filename + " 不存在!");
}
}
/// <summary>
/// 在指定的Xml元素下,添加子Xml元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml元素</param>
/// <param name="childElementName">要添加的Xml元素名称</param>
/// <param name="childElementValue">要添加的Xml元素值</param>
/// <returns></returns>
public bool AppendChildElementByNameValue(ref XmlElement xmlElement, string childElementName, object childElementValue)
{
return AppendChildElementByNameValue(ref xmlElement, childElementName, childElementValue, false);
}
/// <summary>
/// 在指定的Xml元素下,添加子Xml元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml元素</param>
/// <param name="childElementName">要添加的Xml元素名称</param>
/// <param name="childElementValue">要添加的Xml元素值</param>
/// <param name="IsCDataSection">是否是CDataSection类型的子元素</param>
/// <returns></returns>
public bool AppendChildElementByNameValue(ref XmlElement xmlElement, string childElementName, object childElementValue, bool IsCDataSection)
{
if ((xmlElement != null) && (xmlElementOwnerDocument != null))
{
//是否是CData类型Xml元素
if (IsCDataSection)
{
XmlCDataSection tempdata = xmlElementOwnerDocumentCreateCDataSection(childElementName);
tempdataInnerText = FiltrateControlCharacter(childElementValueToString());
XmlElement childXmlElement = xmlElementOwnerDocumentCreateElement(childElementName);
childXmlElementAppendChild(tempdata);
xmlElementAppendChild(childXmlElement);
}
else
{
XmlElement childXmlElement = xmlElementOwnerDocumentCreateElement(childElementName);
childXmlElementInnerText = FiltrateControlCharacter(childElementValueToString());
xmlElementAppendChild(childXmlElement);
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// 在指定的Xml结点下,添加子Xml元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml节点</param>
/// <param name="childElementName">要添加的Xml元素名称</param>
/// <param name="childElementValue">要添加的Xml元素值</param>
/// <returns></returns>
public bool AppendChildElementByNameValue(ref XmlNode xmlNode, string childElementName, object childElementValue)
{
return AppendChildElementByNameValue(ref xmlNode, childElementName, childElementValue, false);
}
/// <summary>
/// 在指定的Xml结点下,添加子Xml元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml节点</param>
/// <param name="childElementName">要添加的Xml元素名称</param>
/// <param name="childElementValue">要添加的Xml元素值</param>
/// <param name="IsCDataSection">是否是CDataSection类型的子元素</param>
/// <returns></returns>
public bool AppendChildElementByNameValue(ref XmlNode xmlNode, string childElementName, object childElementValue, bool IsCDataSection)
{
if ((xmlNode != null) && (xmlNodeOwnerDocument != null))
{
//是否是CData类型Xml结点
if (IsCDataSection)
{
XmlCDataSection tempdata = xmlNodeOwnerDocumentCreateCDataSection(childElementName);
tempdataInnerText = FiltrateControlCharacter(childElementValueToString());
XmlElement childXmlElement = xmlNodeOwnerDocumentCreateElement(childElementName);
childXmlElementAppendChild(tempdata);
xmlNodeAppendChild(childXmlElement);
}
else
{
XmlElement childXmlElement = xmlNodeOwnerDocumentCreateElement(childElementName);
childXmlElementInnerText = FiltrateControlCharacter(childElementValueToString());
xmlNodeAppendChild(childXmlElement);
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// 通过数据行向当前XML元素下追加子元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml元素</param>
/// <param name="dcc">当前数据表中的列集合</param>
/// <param name="dr">当前行数据</param>
/// <returns></returns>
public bool AppendChildElementByDataRow(ref XmlElement xmlElement, DataColumnCollection dcc, DataRow dr)
{
return AppendChildElementByDataRow(ref xmlElement, dcc, dr, null);
}
/// <summary>
/// 通过数据行向当前XML元素下追加子元素
/// </summary>
/// <param name="xmlElement">被追加子元素的Xml元素</param>
/// <param name="dcc">当前数据表中的列集合</param>
/// <param name="dr">当前行数据</param>
/// <param name="removecols">不会被追加的列名</param>
/// <returns></returns>
public bool AppendChildElementByDataRow(ref XmlElement xmlElement, DataColumnCollection dcc, DataRow dr, string removecols)
{
if((xmlElement != null)&&(xmlElementOwnerDocument != null))
{
foreach (DataColumn dc in dcc)
{
if ((removecols == null) ||
(removecols == "") ||
(("," + removecols + ",")ToLower()IndexOf("," + dcCaptionToLower() + ",") < 0))
{
XmlElement tempElement = xmlElementOwnerDocumentCreateElement(dcCaption);
tempElementInnerText = FiltrateControlCharacter(dr[dcCaption]ToString()Trim());
xmlElementAppendChild(tempElement);
}
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// 实始化节点, 当节点存在则清除当前路径下的所有子结点, 如不存在则直接创建该结点
/// </summary>
/// <param name="xmlpath"></param>
/// <returns></returns>
public XmlNode InitializeNode(string xmlpath)
{
XmlNode xmlNode = thisSelectSingleNode(xmlpath);
if (xmlNode != null)
{
xmlNodeRemoveAll();
}
else
{
xmlNode = CreateNode(xmlpath);
}
return xmlNode;
}
/// <summary>
/// 删除指定路径下面的所有子结点和自身
/// </summary>
/// <param name="xmlpath">指定路径</param>
public void RemoveNodeAndChildNode(string xmlpath)
{
XmlNodeList xmlNodeList = thisSelectNodes(xmlpath);
if (xmlNodeListCount > 0)
{
foreach (XmlNode xn in xmlNodeList)
{
xnRemoveAll();
xnParentNodeRemoveChild(xn);
}
}
}
/// <summary>
/// 创建指定路径下的节点
/// </summary>
/// <param name="xmlpath">节点路径</param>
/// <returns></returns>
public XmlNode CreateNode(string xmlpath)
{
string[] xpathArray = xmlpathSplit('/');
string root = "";
XmlNode parentNode = this;
//建立相关节点
for (int i = 1; i < xpathArrayLength; i++)
{
XmlNode node = thisSelectSingleNode(root + "/" + xpathArray[i]);
// 如果当前路径不存在则建立,否则设置当前路径到它的子路径上
if (node == null)
{
XmlElement newElement = thisCreateElement(xpathArray[i]);
parentNodeAppendChild(newElement);
}
//设置低一级的路径
root = root + "/" + xpathArray[i];
parentNode = thisSelectSingleNode(root);
}
return parentNode;
}
/// <summary>
/// 得到指定路径的节点值
/// </summary>
/// <param name="xmlnode">要查找节点</param>
/// <param name="path">指定路径</param>
/// <returns></returns>
public string GetSingleNodeValue(XmlNode xmlnode, string path)
{
if (xmlnode == null)
{
return null;
}
if (xmlnodeSelectSingleNode(path) != null)
{
if (xmlnodeSelectSingleNode(path)LastChild != null)
{
return xmlnodeSelectSingleNode(path)LastChildValue;
}
else
{
return "";
}
}
return null;
}
/// <summary>
/// 过滤控制字符,包括0x00 - 0x08,0x0b - 0x0c,0x0e - 0x1f
/// </summary>
/// <param name="content">要过滤的内容</param>
/// <returns>过滤后的内容</returns>
private string FiltrateControlCharacter(string content)
{
return RegexReplace(content, "[\x00-\x08|\x0b-\x0c|\x0e-\x1f]", "");
}
}
创建解析器
SAXReader saxreader = new SAXReader();
读取文档
Document doc = saxreaderread(new File("url"));
获取根
Element root = docgetRootElement();
获取子节点
List<Element> list = rootelements();
Systemoutprintln(eelementText("name"));
Systemoutprintln(eelement("score")attributeValue("java"));
Reader reader = new InputStreamReader(con
getInputStream());
SAXReader sax = new SAXReader();
// saxsetEncoding("GBK");
Document document = saxread(reader);
documentsetXMLEncoding("GBK");
Element root = documentgetRootElement();
// Document doc = readerread(read);
// Element root = docgetRootElement();
readNode(root, "");
public static void readNode(Element root, String prefix) {
if (root == null) return;
// 获取属性
List<Attribute> attrs = rootattributes();
if (attrs != null && attrssize() > 0) {
Systemerrprint(prefix);
for (Attribute attr : attrs) {
Systemerrprint(attrgetValue() + " ");
}
Systemerrprintln();
}
// 获取他的子节点
List<Element> childNodes = rootelements();
prefix += "\t";
for (Element e : childNodes) {
//输出内容
Systemerrprintln(egetName()+":"+egetData());
readNode(e, prefix);
}
}
读取:
public void GetValue(string value,string date)
{
XmlDocument doc = new XmlDocument();
string path = "";
string title = "";
string body = "";
string bl = "";
path = SystemAppDomainCurrentDomainBaseDirectory;//获取文件路径
docLoad(path + "\\dbGuestxml");
XmlNode node = docSelectSingleNode("dbGuest");
XmlNodeList list = nodeChildNodes;
foreach (XmlNode xn in list)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnf1 = xeChildNodes;
foreach (XmlNode xn2 in xnf1)
{
if (xeGetAttribute("days") == value && xeGetAttribute("date") == date)//读取属性
{
if (xn2Name == "title")//获得标题
{
title = xn2InnerText;
}
if (xn2Name == "body")//获得内容
{
body = xn2InnerText;
}
if (xn2Name == "bl")
{
bl = xn2InnerText;
}
}
}
}
lblmesText = title;
txtbodyText = body;
txtwcdText = bl;
}
保存:
private void btnComfrim_Click(object sender, EventArgs e)
{
string path = "";
// string title = "";
if (txtwcdText == "")
{
lblshowmesText = "任务完成度不能为空";
txtwcdFocus();
lblshowmesText = "";
}
XmlDocument doc = new XmlDocument();
try
{
path = SystemAppDomainCurrentDomainBaseDirectory;//获取文件路径
docLoad(path + "\\dbGuestxml");
XmlNode node = docSelectSingleNode("dbGuest");
XmlNodeList list = nodeChildNodes;
foreach (XmlNode xn in list)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnf1 = xeChildNodes;
foreach (XmlNode xn2 in xnf1)
{
if (xeGetAttribute("days") == valueToString())//读取属性
{
if (xn2Name == "body")//获得内容
{
xn2InnerText = txtbodyText;
}
else if (xn2Name == "bl")
{
xn2InnerText = txtwcdText;
}
if (xn2Name == "title")
{
xn2InnerText = lblmesText;
}
xeSetAttribute("date",DateTimeNowToString("yyyy-MM"));
}
}
}
docSave(path + "\\dbGuestxml");
lblshowmesText = "提示:任务已成功提交!";
}
catch (Exception ex)
{
}
}
根据你的情况去修改下代码吧,这是我之前用的,可对节点进行修改。
应该可以设置id,根据id获取值
不好意思啦!借用zhangmiger代码一下。
XmlDocument doc = new XmlDocument();
docLoad("//XMLFile1xml");
XmlNodeList nodes = docSelectNodes("/Dimap_Document/IMAGING_DATE/vertex");
foreach (XmlNode node in nodes)
{
if(nodeAttributes["id"]value=="v2"){
ConsoleWriteLine(nodeInnerText);
}
}
ConsoleRead();
XMLFile1xml文件的内容:
<xml version="10" encoding="utf-8" >
<Dimap_Document>
<IMAGING_DATE>
<vertex id="v1">
a
</vertex>
<vertex id="v2">
b
</vertex>
<vertex id="v3">
c
</vertex>
</IMAGING_DATE>
</Dimap_Document>
string ids = stringEmpty;
XmlDocument _document = new XmlDocument();
_documentLoad(ServerMapPath("~/XML文件名xml"));
XmlNode _xmlNode = _documentSelectSingleNode("slider");
XmlNode xmlNode = _xmlNodeSelectSingleNode(stringFormat("item[@name=\"{0}\"]", 条件变量名称));
if (xmlNode != null)
{
XmlNodeList xmlNodeList = xmlNodeSelectNodes("join");
foreach (XmlNode item in xmlNodeList)
{
href = GetNodeAttributeValue(item, "href");
//以下节点类似
}
}
private string GetNodeAttributeValue(XmlNode item, string attributeName)
{
if (item == null || itemAttributes[attributeName] == null || stringIsNullOrEmpty(itemAttributes[attributeName]Value))
return stringEmpty;
return itemAttributes[attributeName]Value;
}
以上就是关于求C# 获取xml某个节点值方法全部的内容,包括:求C# 获取xml某个节点值方法、java怎么通过xml节点的属性获取这个节点的值、请高手指点:如何用C# *** 作获取xml文档中的有效节点等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)