c# XML数据解析通用工具, 获取 节点 文本内容
2018-02-11 18:58
567 查看
首先先说明一下 ,节点文本内容获取方法,如:<root>文本内容</root>获取节点文本内容(红色字体部分),假设xmlElement是root节点。上面红色字体的文本其实是root节点的子节点,获取文本内容的方法,就是遍历子节点,判断类型是否为文本内容。方法如下: for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
{
if (xmlElement.ChildNodes[i].NodeType == XmlNodeType.Text)
{
string Text = xmlElement.ChildNodes[i].Value;
}
}搜边全网也没有答案 。。。。首先定义 xml 有用的元素类,每个节点 ,包括:节点属性、子节点列表、子节点名称、子节点内容。如下public class XmlNode
{
/// <summary>
/// 节点属性(不自动装换数据类型)
/// </summary>
public Dictionary<string, string> Property { get; set; }
/// <summary>
/// 节点属性(自动转换数据类型)
/// </summary>
public Dictionary<string, object> PropertyOfType { get; set; }
/// <summary>
/// 子节点列表
/// </summary>
public XmlNode[] Childs { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string Name;
/// <summary>
/// 节点内容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 是否使用自动转换类型
/// </summary>
private bool isUseType;
/// <summary>
/// 初始化Xml节点
/// </summary>
/// <param name="isUseType">是否自动转化类</param>
public XmlNode(bool isUseType =false)
{
this.isUseType = isUseType;
if (isUseType)
{
PropertyOfType = new Dictionary<string, object>();
}
else
{
Property = new Dictionary<string, string>();
}
}
public void XmlSet(XmlElement xmlElement)
{
XmlPropertySet(xmlElement);
XmlTextSet(xmlElement);
XmlNameSet(xmlElement);
}
/// <summary>
/// 设置节点名称
/// </summary>
/// <param name="xmlElement"></param>
private void XmlNameSet(XmlElement xmlElement)
{
Name = xmlElement.Name;
}
/// <summary>
/// 设置属性
/// </summary>
/// <param name="xmlElement"></param>
private void XmlPropertySet(XmlElement xmlElement)
{
XmlAttributeCollection ac = xmlElement.Attributes;
for (int i = 0; i < xmlElement.Attributes.Count; i++)
{
if (isUseType)
{
object value;
// int.TryParse(ac[i].Value, out (int)value) ??bool.TryParse(ac[i].Value)
//ac[i].Value
PropertyOfType.Add(ac[i].Name, ac[i].Value);
}
else
{
Property.Add(ac[i].Name, ac[i].Value);
}
}
}
/// <summary>
/// 设置文本
/// </summary>
/// <param name="xmlElement"></param>
public void XmlTextSet(XmlElement xmlElement)
{
// if (xmlElement.PreviousSibling !=null)
Text = xmlElement.InnerText;
Debug.Log(xmlElement.Name +"/------/"+ Text);
}
/// <summary>
/// 设置所有节点
/// </summary>
/// <param name="Childs"></param>
public void XmlChildsSet(XmlNode[] Childs)
{
this.Childs = Childs;
}
}然后添加文本解析代码:
所有代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
public class XmlParseTool
{
XmlElement xmlNodeRoot;
XmlNode xmlNode;
XmlDocument xmlDocument;
#region 测试脚本
public void readXmlTest()
{
readXml(
"<root>" +
" <child1 attr1='val1' attr2='val2'>" +
"<!--注释-->" +
" text1" +
" </child1>" +
" <child2 attr3='val3'> " +
"<child2_1 attr3='val1'/>" +
"<child2_1 attr3='val2'>3</child2_1>" +
"<child2_1 attr3='val3'>2</child2_1>" +
"text2 " +
"</child2>" +
" </root> ");
///获取root节点--->child1节点---->attr2属性
string child2_1_attr3 = xmlNode.Childs[0].Property["attr2"];
}
#endregion
public void readXml(string xmlContent)
{
xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlContent);
xmlNodeRoot = xmlDocument.DocumentElement;
xmlNode = new XmlNode();
xmlParse(xmlNodeRoot, ref xmlNode);
}
public void xmlRead(string xmlPath)
{
xmlDocument = new XmlDocument();
using (Stream stream = new FileStream(xmlPath, FileMode.Open, FileAccess.Read))
{
xmlDocument.Load(stream);
xmlNodeRoot = xmlDocument.DocumentElement;
xmlNode = new XmlNode();
xmlParse(xmlNodeRoot, ref xmlNode);
stream.Close();
}
}
public void xmlParse(XmlElement xmlElement, ref XmlNode xmlNode)
{
if ((xmlElement as XmlElement) == null)
return;
xmlNode.XmlSet(xmlElement);
if (xmlElement.ChildNodes.Count > 0)
{
XmlNode[] NodeList;
List<XmlElement> xmlNodeList = new List<XmlElement>();
for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
{
if ((xmlElement.ChildNodes[i].NodeType) == XmlNodeType.Element)
{
xmlNodeList.Add(xmlElement.ChildNodes[i] as XmlElement);
}
}
NodeList = new XmlNode[xmlNodeList.Count];
xmlNode.XmlChildsSet(NodeList);
for (int i = 0; i < xmlNodeList.Count; i++)
{
//if ((xmlElement.ChildNodes[i].NodeType) != XmlNodeType.Element)
//{
// continue;
//}
XmlElement xmlElementChild = (XmlElement)xmlElement.ChildNodes[i];
XmlNode xmlChildNode = new XmlNode();
NodeList[i] = xmlChildNode;
if (xmlElementChild.ChildNodes.Count > 0)
xmlParse((XmlElement)xmlElement.ChildNodes[i], ref xmlChildNode);
}
}
else
{
//xmlNode.XmlTextSet(xmlElement);
}
}
}
public class XmlNode
{
/// <summary>
/// 节点属性(不自动装换数据类型)
/// </summary>
public Dictionary<string, string> Property { get; set; }
/// <summary>
/// 节点属性(自动转换数据类型)
/// </summary>
public Dictionary<string, object> PropertyOfType { get; set; }
/// <summary>
/// 子节点列表
/// </summary>
public XmlNode[] Childs { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string Name;
/// <summary>
/// 节点内容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 是否使用自动转换类型
/// </summary>
private bool isUseType;
/// <summary>
/// 初始化Xml节点
/// </summary>
/// <param name="isUseType">是否自动转化类</param>
public XmlNode(bool isUseType =false)
{
this.isUseType = isUseType;
if (isUseType)
{
PropertyOfType = new Dictionary<string, object>();
}
else
{
Property = new Dictionary<string, string>();
}
}
public void XmlSet(XmlElement xmlElement)
{
XmlPropertySet(xmlElement);
XmlTextSet(xmlElement);
XmlNameSet(xmlElement);
}
/// <summary>
/// 设置节点名称
/// </summary>
/// <param name="xmlElement"></param>
private void XmlNameSet(XmlElement xmlElement)
{
Name = xmlElement.Name;
}
/// <summary>
/// 设置属性
/// </summary>
/// <param name="xmlElement"></param>
private void XmlPropertySet(XmlElement xmlElement)
{
XmlAttributeCollection ac = xmlElement.Attributes;
for (int i = 0; i < xmlElement.Attributes.Count; i++)
{
if (isUseType)
{
object value;
// int.TryParse(ac[i].Value, out (int)value) ??bool.TryParse(ac[i].Value)
//ac[i].Value
PropertyOfType.Add(ac[i].Name, ac[i].Value);
}
else
{
Property.Add(ac[i].Name, ac[i].Value);
}
}
}
/// <summary>
/// 设置文本
/// </summary>
/// <param name="xmlElement"></param>
public void XmlTextSet(XmlElement xmlElement)
{
// if (xmlElement.PreviousSibling !=null)
for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
{
if (xmlElement.ChildNodes[i].NodeType == XmlNodeType.Text)
{
Text = xmlElement.ChildNodes[i].Value;
}
}
}
/// <summary>
/// 设置所有节点
/// </summary>
/// <param name="Childs"></param>
public void XmlChildsSet(XmlNode[] Childs)
{
this.Childs = Childs;
}
}
http://mp.blog.csdn.net/postedit/79312699
内容转载请标注。尊重他人劳动 ,谢谢
{
if (xmlElement.ChildNodes[i].NodeType == XmlNodeType.Text)
{
string Text = xmlElement.ChildNodes[i].Value;
}
}搜边全网也没有答案 。。。。首先定义 xml 有用的元素类,每个节点 ,包括:节点属性、子节点列表、子节点名称、子节点内容。如下public class XmlNode
{
/// <summary>
/// 节点属性(不自动装换数据类型)
/// </summary>
public Dictionary<string, string> Property { get; set; }
/// <summary>
/// 节点属性(自动转换数据类型)
/// </summary>
public Dictionary<string, object> PropertyOfType { get; set; }
/// <summary>
/// 子节点列表
/// </summary>
public XmlNode[] Childs { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string Name;
/// <summary>
/// 节点内容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 是否使用自动转换类型
/// </summary>
private bool isUseType;
/// <summary>
/// 初始化Xml节点
/// </summary>
/// <param name="isUseType">是否自动转化类</param>
public XmlNode(bool isUseType =false)
{
this.isUseType = isUseType;
if (isUseType)
{
PropertyOfType = new Dictionary<string, object>();
}
else
{
Property = new Dictionary<string, string>();
}
}
public void XmlSet(XmlElement xmlElement)
{
XmlPropertySet(xmlElement);
XmlTextSet(xmlElement);
XmlNameSet(xmlElement);
}
/// <summary>
/// 设置节点名称
/// </summary>
/// <param name="xmlElement"></param>
private void XmlNameSet(XmlElement xmlElement)
{
Name = xmlElement.Name;
}
/// <summary>
/// 设置属性
/// </summary>
/// <param name="xmlElement"></param>
private void XmlPropertySet(XmlElement xmlElement)
{
XmlAttributeCollection ac = xmlElement.Attributes;
for (int i = 0; i < xmlElement.Attributes.Count; i++)
{
if (isUseType)
{
object value;
// int.TryParse(ac[i].Value, out (int)value) ??bool.TryParse(ac[i].Value)
//ac[i].Value
PropertyOfType.Add(ac[i].Name, ac[i].Value);
}
else
{
Property.Add(ac[i].Name, ac[i].Value);
}
}
}
/// <summary>
/// 设置文本
/// </summary>
/// <param name="xmlElement"></param>
public void XmlTextSet(XmlElement xmlElement)
{
// if (xmlElement.PreviousSibling !=null)
Text = xmlElement.InnerText;
Debug.Log(xmlElement.Name +"/------/"+ Text);
}
/// <summary>
/// 设置所有节点
/// </summary>
/// <param name="Childs"></param>
public void XmlChildsSet(XmlNode[] Childs)
{
this.Childs = Childs;
}
}然后添加文本解析代码:
public void xmlParse(XmlElement xmlElement, ref XmlNode xmlNode) { if ((xmlElement as XmlElement) == null) return; xmlNode.XmlSet(xmlElement); if (xmlElement.ChildNodes.Count > 0) { XmlNode[] NodeList; List<XmlElement> xmlNodeList = new List<XmlElement>(); for (int i = 0; i < xmlElement.ChildNodes.Count; i++) { if ((xmlElement.ChildNodes[i].NodeType) == XmlNodeType.Element) { xmlNodeList.Add(xmlElement.ChildNodes[i] as XmlElement); } } NodeList = new XmlNode[xmlNodeList.Count]; xmlNode.XmlChildsSet(NodeList); for (int i = 0; i < xmlNodeList.Count; i++) { //if ((xmlElement.ChildNodes[i].NodeType) != XmlNodeType.Element) //{ // continue; //} XmlElement xmlElementChild = (XmlElement)xmlElement.ChildNodes[i]; XmlNode xmlChildNode = new XmlNode(); NodeList[i] = xmlChildNode; if (xmlElementChild.ChildNodes.Count > 0) xmlParse((XmlElement)xmlElement.ChildNodes[i], ref xmlChildNode); } } else { //xmlNode.XmlTextSet(xmlElement); } }
所有代码如下:using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using System.IO;
public class XmlParseTool
{
XmlElement xmlNodeRoot;
XmlNode xmlNode;
XmlDocument xmlDocument;
#region 测试脚本
public void readXmlTest()
{
readXml(
"<root>" +
" <child1 attr1='val1' attr2='val2'>" +
"<!--注释-->" +
" text1" +
" </child1>" +
" <child2 attr3='val3'> " +
"<child2_1 attr3='val1'/>" +
"<child2_1 attr3='val2'>3</child2_1>" +
"<child2_1 attr3='val3'>2</child2_1>" +
"text2 " +
"</child2>" +
" </root> ");
///获取root节点--->child1节点---->attr2属性
string child2_1_attr3 = xmlNode.Childs[0].Property["attr2"];
}
#endregion
public void readXml(string xmlContent)
{
xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlContent);
xmlNodeRoot = xmlDocument.DocumentElement;
xmlNode = new XmlNode();
xmlParse(xmlNodeRoot, ref xmlNode);
}
public void xmlRead(string xmlPath)
{
xmlDocument = new XmlDocument();
using (Stream stream = new FileStream(xmlPath, FileMode.Open, FileAccess.Read))
{
xmlDocument.Load(stream);
xmlNodeRoot = xmlDocument.DocumentElement;
xmlNode = new XmlNode();
xmlParse(xmlNodeRoot, ref xmlNode);
stream.Close();
}
}
public void xmlParse(XmlElement xmlElement, ref XmlNode xmlNode)
{
if ((xmlElement as XmlElement) == null)
return;
xmlNode.XmlSet(xmlElement);
if (xmlElement.ChildNodes.Count > 0)
{
XmlNode[] NodeList;
List<XmlElement> xmlNodeList = new List<XmlElement>();
for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
{
if ((xmlElement.ChildNodes[i].NodeType) == XmlNodeType.Element)
{
xmlNodeList.Add(xmlElement.ChildNodes[i] as XmlElement);
}
}
NodeList = new XmlNode[xmlNodeList.Count];
xmlNode.XmlChildsSet(NodeList);
for (int i = 0; i < xmlNodeList.Count; i++)
{
//if ((xmlElement.ChildNodes[i].NodeType) != XmlNodeType.Element)
//{
// continue;
//}
XmlElement xmlElementChild = (XmlElement)xmlElement.ChildNodes[i];
XmlNode xmlChildNode = new XmlNode();
NodeList[i] = xmlChildNode;
if (xmlElementChild.ChildNodes.Count > 0)
xmlParse((XmlElement)xmlElement.ChildNodes[i], ref xmlChildNode);
}
}
else
{
//xmlNode.XmlTextSet(xmlElement);
}
}
}
public class XmlNode
{
/// <summary>
/// 节点属性(不自动装换数据类型)
/// </summary>
public Dictionary<string, string> Property { get; set; }
/// <summary>
/// 节点属性(自动转换数据类型)
/// </summary>
public Dictionary<string, object> PropertyOfType { get; set; }
/// <summary>
/// 子节点列表
/// </summary>
public XmlNode[] Childs { get; set; }
/// <summary>
/// 节点名称
/// </summary>
public string Name;
/// <summary>
/// 节点内容
/// </summary>
public string Text { get; set; }
/// <summary>
/// 是否使用自动转换类型
/// </summary>
private bool isUseType;
/// <summary>
/// 初始化Xml节点
/// </summary>
/// <param name="isUseType">是否自动转化类</param>
public XmlNode(bool isUseType =false)
{
this.isUseType = isUseType;
if (isUseType)
{
PropertyOfType = new Dictionary<string, object>();
}
else
{
Property = new Dictionary<string, string>();
}
}
public void XmlSet(XmlElement xmlElement)
{
XmlPropertySet(xmlElement);
XmlTextSet(xmlElement);
XmlNameSet(xmlElement);
}
/// <summary>
/// 设置节点名称
/// </summary>
/// <param name="xmlElement"></param>
private void XmlNameSet(XmlElement xmlElement)
{
Name = xmlElement.Name;
}
/// <summary>
/// 设置属性
/// </summary>
/// <param name="xmlElement"></param>
private void XmlPropertySet(XmlElement xmlElement)
{
XmlAttributeCollection ac = xmlElement.Attributes;
for (int i = 0; i < xmlElement.Attributes.Count; i++)
{
if (isUseType)
{
object value;
// int.TryParse(ac[i].Value, out (int)value) ??bool.TryParse(ac[i].Value)
//ac[i].Value
PropertyOfType.Add(ac[i].Name, ac[i].Value);
}
else
{
Property.Add(ac[i].Name, ac[i].Value);
}
}
}
/// <summary>
/// 设置文本
/// </summary>
/// <param name="xmlElement"></param>
public void XmlTextSet(XmlElement xmlElement)
{
// if (xmlElement.PreviousSibling !=null)
for (int i = 0; i < xmlElement.ChildNodes.Count; i++)
{
if (xmlElement.ChildNodes[i].NodeType == XmlNodeType.Text)
{
Text = xmlElement.ChildNodes[i].Value;
}
}
}
/// <summary>
/// 设置所有节点
/// </summary>
/// <param name="Childs"></param>
public void XmlChildsSet(XmlNode[] Childs)
{
this.Childs = Childs;
}
}
http://mp.blog.csdn.net/postedit/79312699
内容转载请标注。尊重他人劳动 ,谢谢
相关文章推荐
- Java接收Cordys中webservice接口的返回数据并解析xml获取相应节点数据
- box2d 描点工具生成b2Shape数据(xml通用格式),oc、c++解析模块类文件,b2Shape数据可视化工具
- 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析
- 获取xml中的 (一部分报文节点)循环组的文本内容
- 解析XML获取节点内容
- box2d 描点工具生成b2Shape数据(xml通用格式),oc、c++解析模块类文件,b2Shape数据可视化工具
- C# 解析带前缀的Xml节点内容
- java 将xml格式的文本生成xml文件,并获取xml中指定节点的内容
- C# 解析带前缀的Xml节点内容
- Dom4j解析XML文本,遍历指定节点下的节点内容和属性,并存入List<Map<String,Object>
- C# 解析带前缀的Xml节点内容
- C#解析xml(获取指定节点值)
- C#,xml、html节点数据解析
- 读取XML示例:C#获取XML的数据
- ASP.NET解析xml返回某节点属性的内容
- C#读取XML节点内容方法
- C#中用XmlDocument对象获取XML文件中的节点值
- C#中用XmlDocument对象获取带名命空间XML文件的节点值
- javascript获取元素文本内容的通用函数
- C#读取xml节点数据