您的位置:首页 > 编程语言 > C#

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;
}
}然后添加文本解析代码:
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
内容转载请标注。尊重他人劳动 ,谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息