private void CreatSetMethod1(NodeName nona, string xmlpartent, int k, StreamWriter sw) { k++; string rootname = nona.CurrentName; //写自身的属性信息 code_StringArray.Add(string.Format(" XmlElement xml{0} = xmldoc.CreateElement( \"{1}\");", rootname.ToLower(), rootname)); int j = 0; //TitleInfo _titleinfo in _title.TitleInfoList code_StringArray.Add(string.Format("foreach({0} _{1} in _{2}.{3}List)", rootname, rootname.ToLower(), xmlpartent, rootname)); code_StringArray.Add("{"); foreach (string att in nona.AttributeNames) { string attname = nona.AttributeNames[j]; code_StringArray.Add(string.Format("xml{0}.SetAttribute(\"{1}\", _{2}.{3});", rootname.ToLower(), attname, rootname.ToLower(), attname)); j++; } code_StringArray.Add("}"); //寻找与子节点名称相同的节点 foreach (string nnchild in nona.CnNames) { foreach (NodeName nn in nnlist) { if (nn.CurrentName == nnchild) { NodeName nodecutrrent = nn; string xmlchild = "xml" + nnchild.ToLower(); code_StringArray.Add(string.Format("foreach({0} _{1} in _{2}.{3}List)", nnchild, nnchild.ToLower(), rootname.ToLower(), nnchild)); code_StringArray.Add("{"); code_StringArray.Add(string.Format(" XmlElement {0} = xmldoc.CreateElement( \"{1}\");", xmlchild, nnchild)); int i = 0; foreach (string att in nn.AttributeNames) { string attname = nn.AttributeNames[i]; code_StringArray.Add(string.Format("xmlroot.SetAttribute(\"{0}\", {1}.{2});", attname, nnchild, attname)); i++; } foreach (string nnchild1 in nodecutrrent.CnNames) { foreach (NodeName nn1 in nnlist) { if (nn1.CurrentName == nnchild1) { string xmlchild1 = "xml" + nnchild1.ToLower(); CreatSetMethod1(nn1, xmlchild1, k, sw); } } } code_StringArray.Add(string.Format(" {0}.AppendChild({1});", xmlpartent, xmlchild)); code_StringArray.Add("}"); } } } }
/// <summary> /// 获取所有的实体类 /// </summary> /// <param name="classname"></param> /// <param name="rootnode"></param> /// <param name="sw"></param> private void CreatClass(string classname, XmlNode rootnode) { //如果该类已经存在,则不创建 if (!classnamelist.Contains(classname)) { classnamelist.Add(classname); NodeName nn = new NodeName(); List <string> attlist = new List <string>(); List <string> cnlist = new List <string>(); nn.CurrentName = rootnode.Name; code_StringArray.Add(string.Format(" public class {0}", classname)); code_StringArray.Add(" {"); if (rootnode.Attributes != null) { foreach (XmlAttribute att in rootnode.Attributes) { if (!attlist.Contains(att.Name)) { attlist.Add(att.Name); string wl = string.Format(" public string {0}", att.Name); code_StringArray.Add(wl + "{get;set;}"); } } } if (rootnode.ChildNodes != null) { foreach (XmlNode child in rootnode.ChildNodes) { if (!cnlist.Contains(child.Name)) { cnlist.Add(child.Name); string wl = string.Format(" public List<{0}> {1}List ", child.Name, child.Name); code_StringArray.Add(wl + "{get;set;}"); } } } nn.AttributeNames = attlist; nn.CnNames = cnlist; nnlist.Add(nn); code_StringArray.Add(" }"); } foreach (XmlNode child in rootnode.ChildNodes) { CreatClass(child.Name, child); } }
private void CreatSetMethHeader1(List <NodeName> nonalist, StreamWriter sw) { //StreamWriter sw = new StreamWriter(csfullname, true, Encoding.UTF8); NodeName nnroot = nonalist[0]; string rootname = nnroot.CurrentName; code_StringArray.Add(string.Format(" public void SetXMLInfo({0} _{1}, string xmlpath)", rootname, rootname.ToLower())); code_StringArray.Add(" {"); code_StringArray.Add(" XmlDocument xmldoc = new XmlDocument();"); code_StringArray.Add(" XmlDeclaration xmldecl;"); code_StringArray.Add(" xmldecl = xmldoc.CreateXmlDeclaration(\"1.0\", \"gb2312\", null);"); code_StringArray.Add(" xmldoc.AppendChild(xmldecl);"); //code_StringArray.Add(" XmlElement xmlroot = xmldoc.CreateElement( \"{0}\");", rootname); //int i = 0; //foreach (string att in nonalist[0].AttributeNames) //{ // string attname = nnroot.AttributeNames[i]; // code_StringArray.Add("xmlroot.SetAttribute(\"{0}\", {1}.{2});", attname, rootname, attname); // i++; //} //code_StringArray.Add(" xmldoc.AppendChild(xmlroot);"); //sw.Flush(); //sw.Close(); }