/// <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]", "");
}
}
public void AA()
{
XmlDocument doc=new XmlDocument();
XmlElement xmlel = docDocumentElement;
DD(xmlel);
} public void DD(XmlNode node)
{
foreach (XmlNode node1 in nodeChildNodes)
{
if (node1ChildNodesCount > 1)
{
DD(node1);
}
else
{
ConsoleWriteLine(node1InnerText);
}
}
}
网上有个Cmarkup类,你可以下一个。
你用迭代法就可以遍历了。
过程如下:
函数()
{
if(找到下一个节点)
{
进入节点
函数()
跳出节点
函数()
}
}
用微软的 DOM,
MSXML4
//引入msxml4dll
#import "C:/WINNT0/system32/msxml4dll"
//创建XMLDOMDocument指针
MSXML2::IXMLDOMDocumentPtr pXMLDoc;
// 初始化COM接口
::CoInitialize(NULL);
// 通过智能指针创建一个解析器的实例
HRESULT hr;
hr =pXMLDocCreateInstance(__uuidof(MSXML2::DOMDocument30));
//加载文件
pXMLDoc->load("f://hexml");
//在树中查找名为City的节点,"//"表示在任意一层查找
MSXML2::IXMLDOMElementPtr childNode ;
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//AUTHOR"));
//得到节点类型
MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);
//节点名称
BSTR var;
CString name;
childNode->get_nodeName(&var);
name = (char)(_bstr_t)var;
//节点值
VARIANT varVal;
childNode->get_nodeTypedValue(&varVal);
CString strValue = (char)(_bstr_t)varVal;
读属性:
//节点属性,放在链表中
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs = NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode = (MSXML2::IXMLDOMElementPtr)(pXMLDoc->selectSingleNode("//NUM"));
childNode->get_attributes(&pAttrs);
long nCount ;
pAttrs->get_length(&nCount);
for(int i = 0 ; i < nCount ; i++)
{
pAttrs->get_item(i,&pAttrItem);
//我们可以通过函数get_nodeName,get_nodeTypedValue得到属性名和属性值
//也可以直接得到
CString strAttrName = (char)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue = (char)(_bstr_t)pAttrItem->nodeTypedValue;
}
另外
可以用MSXML DOM
创建一个文档对象的过程。
HRESULT hr;
IXMLDomDocument pXMLDoc;
IXMLDOMNode pXDN;
Hr=CoInitialize(NULL); //COM的初始化
hr=CoCreateInstance(CLSID_DOM Document,NULL,CLSCTX_INPPROC_SERVER,
IID_IXMLDOMDocument,(void) &pXMLDoc);
hr=pXMLDoc->QueryInterface (IID_IXMLDOMNode,(void)&pXDN);
使用文档中的createElement方法来创建一个节点装载和保存XML文件。通过load或者是loadXML方法可以从一个指定的URL来装载一个XML文档Save方法是用来把文档保存到一个指定的位置,用getElementsByTagName获得制定节点数据
二、
// 创建对象
CoInitialize(NULL); // 初始化COM
// 创建解析器实例
CComPtr<IXMLDOMDocument> spXMLDOM;
BSTR bstrItemText;
// long value;
CString strbuf;
HRESULT hr=spXMLDOMCoCreateInstance (_uuidof(DOMDocument));
spXMLDOM->put_async(VARIANT_FALSE);
spXMLDOM->put_validateOnParse(VARIANT_FALSE);
spXMLDOM->put_resolveExternals(VARIANT_FALSE);
spXMLDOM->put_preserveWhiteSpace(VARIANT_TRUE);
_bstr_t path = _bstr_t(xmlPath);
// 装载XML文档。
hr=spXMLDOM->load(path);
// 1、读取里面全部文本
spXMLDOM->get_text(&bstrItemText); // new
strbuf = bstrItemText; // new
// 2、读取一个节点
_bstr_t bstrSS("paragraph");
CComPtr<IXMLDOMNodeList> spParagraphs;
spParagraphs=spXMLDOM->getElementsByTagName(bstrSS); //搜索"Paragrah"
//是否正确的得到了指向IDOMNodeList的指针。
hr = spParagraphs->get_length(&value); //得到所包含的NODE节点的个数
if(SUCCEEDED(hr))
{
spParagraphs->reset();
for(int ii=0; ii<value; ii++)
{
CComPtr<IXMLDOMNode> spParagraph;
//得到具体的一个NODE节点
// hr = spParagraphs->get_item(ii,&spParagraph);
spParagraph = spParagraphs->nextNode();
if(SUCCEEDED(hr))
{
spParagraph->get_text(&bstrItemText); //得到该节点相关的文本信息
strbuf += bstrItemText;
strbuf += " ";
}
}
}
XmlDocument XD = new XmlDocument();
XDLoad(加载路径);
XmlNode XN = XDLastChildLastChild;
MessageShow(XNInnerText);
文件内容:
<xml version="10" encoding="gb2312">
<Od>
<!--章节名称-->
<CapterName>1</CapterName>
<CapterName>2</CapterName>
<CapterName>3</CapterName>
<CapterName>4</CapterName>
<CapterName>5</CapterName>
<CapterName>6</CapterName>
</Od>
输出结果为:6
以上就是关于求C# 获取xml某个节点值方法全部的内容,包括:求C# 获取xml某个节点值方法、C#:如何用递归读出XML文件所有的节点的值、如何通过VC++读取XML所有数据(所有数据未知)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)