private void ReadNode(XmlReader reader, IConfigSection section) { while (true) { var remark = ""; if (reader.NodeType == XmlNodeType.Comment) { remark = reader.Value; } while (reader.NodeType == XmlNodeType.Comment || reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } if (reader.NodeType != XmlNodeType.Element) { break; } var name = reader.Name; var cfg = section.AddChild(name); // 前一行是注释 if (!remark.IsNullOrEmpty()) { cfg.Comment = remark; } // 读取属性值 if (reader.HasAttributes) { reader.MoveToFirstAttribute(); do { var cfg2 = cfg.AddChild(reader.Name); cfg2.Value = reader.Value; } while (reader.MoveToNextAttribute()); } else { reader.ReadStartElement(); } while (reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } // 遇到下一层节点 if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.Comment) { ReadNode(reader, cfg); } else if (reader.NodeType == XmlNodeType.Text) { cfg.Value = reader.ReadContentAsString(); } if (reader.NodeType == XmlNodeType.Attribute) { reader.Read(); } if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } while (reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } } }
private void ReadNode(XmlReader reader, IConfigSection section) { while (true) { var remark = ""; if (reader.NodeType == XmlNodeType.Comment) { remark = reader.Value; } while (reader.NodeType == XmlNodeType.Comment || reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } if (reader.NodeType != XmlNodeType.Element) { break; } var name = reader.Name; var cfg = section.AddChild(name); // 前一行是注释 if (!remark.IsNullOrEmpty()) { cfg.Comment = remark; } // 读取属性值 if (reader.HasAttributes) { var dic = new Dictionary <String, String>(); reader.MoveToFirstAttribute(); do { //var cfg2 = cfg.AddChild(reader.Name); //cfg2.Value = reader.Value; dic[reader.Name] = reader.Value; } while (reader.MoveToNextAttribute()); // 如果只有一个Value属性,可能是基元类型数组 if (dic.Count == 1 && dic.TryGetValue("Value", out var val)) { cfg.Value = val; } else { foreach (var item in dic) { var cfg2 = cfg.AddChild(item.Key); cfg2.Value = item.Value; } } } else { reader.ReadStartElement(); } while (reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } // 遇到下一层节点 if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.Comment) { ReadNode(reader, cfg); } else if (reader.NodeType == XmlNodeType.Text) { cfg.Value = reader.ReadContentAsString(); } if (reader.NodeType == XmlNodeType.Attribute) { reader.Read(); } if (reader.NodeType == XmlNodeType.EndElement) { reader.ReadEndElement(); } while (reader.NodeType == XmlNodeType.Whitespace) { reader.Skip(); } } }