Exemple #1
0
 private static void foreach_xml_data_children(Xml_Data data, XmlElement root, XmlDocument doc)
 {
     if (data.xml_children != null)
     {
         foreach (Xml_Data child in data.xml_children)
         {
             XmlElement xe = doc.CreateElement(child.xml_node);
             if (child.xml_attr_val.Count() > 0)
             {
                 foreach (Xml_Attr_Value one in child.xml_attr_val)
                 {
                     xe.SetAttribute(one.xml_Attribute, one.xml_value);
                 }
             }
             root.AppendChild(xe);
             foreach_xml_data_children(child, xe, doc);
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// 将对象转换成xml格式的字符串
        /// </summary>
        /// <param name="data"></param>

        /* 示例
         * 传入  Xml_Data xml_data = new Xml_Data()
         *  {
         *      xml_node = "LINES",
         *      xml_children = new Xml_Data[] {
         *         new Xml_Data(){
         *           xml_node="LINE",
         *           xml_attr_val=new Xml_Attr_Value[]{ new Xml_Attr_Value(){xml_Attribute="no", xml_value="3"}},
         *           xml_children=new Xml_Data[]{
         *              new Xml_Data(){ xml_node="STAGE", xml_attr_val=new Xml_Attr_Value[]{ new Xml_Attr_Value(){ xml_Attribute="pwd",xml_value="bw131515"},new Xml_Attr_Value(){ xml_Attribute="user", xml_value="bw"}}},
         *              new Xml_Data(){ xml_node="STAGE",xml_attr_val=new Xml_Attr_Value[]{ new Xml_Attr_Value(){ xml_Attribute="pwd",xml_value="bw131515"},new Xml_Attr_Value(){ xml_Attribute="user", xml_value="bw"}}},
         *              new Xml_Data(){ xml_node="STAGE",xml_attr_val=new Xml_Attr_Value[]{ new Xml_Attr_Value(){ xml_Attribute="pwd",xml_value="bw131515"},new Xml_Attr_Value(){ xml_Attribute="user", xml_value="bw"}}},
         *              new Xml_Data(){ xml_node="STAGE",xml_attr_val=new Xml_Attr_Value[]{ new Xml_Attr_Value(){ xml_Attribute="pwd",xml_value="bw131515"},new Xml_Attr_Value(){ xml_Attribute="user", xml_value="bw"}}}
         *           }
         *         }
         *      }
         *      //xml_attr_val = new Xml_Attr_Value[] {
         *      //    new Xml_Attr_Value() { xml_Attribute = "", xml_value = "封笑笑" },
         *      //    new Xml_Attr_Value() { xml_Attribute = "OtherName", xml_value = "封大侠" } },
         *      //xml_children = new Xml_Data[] { new Xml_Data() {
         *
         *
         *      //} }
         *  };
         * 输出
         * <?xml version="1.0" encoding="GB2312"?>
         * <LINES>
         *   <LINE no="3">
         *        <STAGE pwd="bw131515" user="******" />
         *        <STAGE pwd="bw131515" user="******" />
         *        <STAGE pwd="bw131515" user="******" />
         *        <STAGE pwd="bw131515" user="******" />
         *   </LINE>
         * </LINES>
         */
        public static void StringToXml(Xml_Data data)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode     xn  = doc.CreateXmlDeclaration("1.0", "GB2312", null);

            doc.AppendChild(xn);
            XmlElement root = doc.CreateElement(data.xml_node);

            if (data.xml_attr_val != null)
            {
                foreach (Xml_Attr_Value one in data.xml_attr_val)
                {
                    root.SetAttribute(one.xml_Attribute, one.xml_value);
                }
            }
            doc.AppendChild(root);
            foreach_xml_data_children(data, root, doc);
            string p = doc.InnerXml;

            doc.Save(@"D:\p.xml");
        }