private XmlParse ParseXml(Action <XmlDocument> loadXml, bool isSort, Action <XmlParseNode> parseNode, ref XmlDocument xmlDocument) { if (loadXml == null) { return(null); } try { xmlDocument = new XmlDocument(); loadXml(xmlDocument); if (xmlDocument.HasChildNodes) { var xmlparseNode = new XmlParseNode(); ParseXmlNodes(xmlparseNode, xmlDocument.ChildNodes, isSort, parseNode); if (xmlparseNode.NodeCollection != null && xmlparseNode.NodeCollection.Count > 0) { return new XmlParse { NodeCollection = xmlparseNode.NodeCollection } } ; } } catch { xmlDocument = null; } return(null); }
private void ParseNode(string name, string value, Action <XmlParseNode> parseNode, ref XmlParseNode xmlParseNode) { if (xmlParseNode == null) { xmlParseNode = new XmlParseNode(); } xmlParseNode.OriginalName = name; xmlParseNode.DisplayName = name; xmlParseNode.Value = string.IsNullOrEmpty(value) ? string.Empty : value; if (parseNode != null) { parseNode(xmlParseNode); } }
/// <summary> /// /// </summary> /// <param name="parentNode">父节点</param> /// <param name="xmlNodeList">Xml子节点集合</param> /// <param name="isSort">是否针对属性和节点进行排序</param> /// <param name="parseNode">自定义构造Attrribute或Node,一般用于决策数据类型或隐藏,参数为null,则所有Value按照字符串处理且均显示</param> private void ParseXmlNodes(XmlParseNode parentNode, XmlNodeList xmlNodeList, bool isSort, Action <XmlParseNode> parseNode) { if (xmlNodeList == null || parentNode == null) { return; } if (parentNode.NodeCollection == null) { parentNode.NodeCollection = new ObservableCollection <XmlParseNode>(); } foreach (XmlNode node in xmlNodeList) { //从有效节点开始 if (node.NodeType != XmlNodeType.Element) { continue; } var xmlParseNode = new XmlParseNode { XmlNode = node, Parent = parentNode }; if (node.HasChildNodes && node.ChildNodes.Count == 1 && node.FirstChild.NodeType == XmlNodeType.Text) { /* <Node>Text</Node> */ ParseNode(node.Name, node.FirstChild.Value, parseNode, ref xmlParseNode); } else { ParseNode(node.Name, node.Value, parseNode, ref xmlParseNode); if (node.HasChildNodes && node.ChildNodes.Count > 0) { ParseXmlNodes(xmlParseNode, node.ChildNodes, isSort, parseNode); } } //解析 Attribute if (node.Attributes != null && node.Attributes.Count > 0) { if (xmlParseNode.NodeCollection == null) { xmlParseNode.NodeCollection = new ObservableCollection <XmlParseNode>(); } foreach (XmlAttribute attribute in node.Attributes) { var xmlParseAttributeNode = new XmlParseNode { IsAttribute = true, XmlNode = attribute, Parent = xmlParseNode }; ParseNode(attribute.Name, attribute.Value, parseNode, ref xmlParseAttributeNode); xmlParseNode.NodeCollection.Add(xmlParseAttributeNode); } } //排序 if (xmlParseNode.NodeCollection != null) { IOrderedEnumerable <XmlParseNode> orderedEnumerable = null; if (!xmlParseNode.NodeCollection.All(n => n.IsAttribute)) { orderedEnumerable = xmlParseNode.NodeCollection.OrderByDescending(n => n.IsAttribute); } if (isSort) { if (orderedEnumerable != null) { orderedEnumerable = orderedEnumerable.ThenBy(n => n.DisplayName); } else { orderedEnumerable = xmlParseNode.NodeCollection.OrderBy(n => n.DisplayName); } } if (orderedEnumerable != null) { xmlParseNode.NodeCollection = new ObservableCollection <XmlParseNode>(orderedEnumerable); } } parentNode.NodeCollection.Add(xmlParseNode); } }