private static string BulidClass(string xmlStr) { //获取XML节点名 var xml = new XmlDocument(); xml.LoadXml(xmlStr); var rootNode = xml.DocumentElement; var str = BulidChildClass(rootNode).ToString(); return str; }
private static StringBuilder BulidChildClass(XmlElement rootNode) { //&& rootNode.FirstChild.LocalName!="#text" var classStr = new StringBuilder(); var endClassStr = new StringBuilder(); endClassStr.Append("}\r\n\r\n"); classStr.Append("public class " + rootNode.LocalName + "\r\n{\r\n"); if (rootNode.HasChildNodes) { foreach (XmlNode node in rootNode.ChildNodes) { if (node.HasChildNodes && node.FirstChild.LocalName != "#text") { classStr.Append(" public " + node.LocalName + " " + node.LocalName + "{get;set;} \r\n\r\n"); //递归 endClassStr.Append(BulidChildClass((XmlElement)node)); } else { classStr.Append(" public string " + node.LocalName + "{get;set;} \r\n\r\n"); }