zoukankan      html  css  js  c++  java
  • C# 生成XML 多级节点

     直接上代码:

     在应用程序中

     class Program
        {
    
            //public static JsonServiceClient Service = new JsonServiceClient(ConfigInfo.GetSettingAsString("ServiceJsonServiceClient"));
    
            public static JsonServiceClient Service = new JsonServiceClient("http://localhost:65241/");
            static void Main(string[] args)
            {
                Console.WriteLine("---------------开始 执行生成XML---------------");
    
                CreateXmlFile();
    
                Console.WriteLine("---------------结束 执行生成XML---------------");
    
                Console.ReadKey();
            }
    
            /// <summary>
            /// 创建
            /// </summary>
            public static void CreateXmlFile()
            {
                XmlDocument xmlDoc = new XmlDocument();
                //创建类型声明节点  
                XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
                xmlDoc.AppendChild(node);
    
                //创建根节点  
                XmlElement xeRoot = xmlDoc.CreateElement("urlset");
                //给节点属性赋值
                xeRoot.SetAttribute("version", "1.0");
                xeRoot.SetAttribute("name", "商品数据");
                xmlDoc.AppendChild(xeRoot);
                zgqp315_Common_Goods_XML_Status status = Service.Get<zgqp315_Common_Goods_XML_Status>(string.Format("selectzgqp315_Common_Goods_XML_Operation/{0}/{1}", 1, 10000));
    
                if (status.List.Count > 0)
                {
                    for (int i = 0; i < status.List.Count; i++)
                    {
                        xeRoot = xmlDoc.CreateElement("url");
                        XmlNode xn_urlset = xmlDoc.SelectSingleNode("urlset");
                        if (xn_urlset != null)
                        {
                            xn_urlset.AppendChild(xeRoot);
                        }
    
                        CreateNode(xmlDoc, xeRoot, "loc", "http://www.baidu.com");
                        CreateNode(xmlDoc, xeRoot, "lastmod", DateTime.Now.ToLocalTime().ToString());
                        CreateNode(xmlDoc, xeRoot, "changefreq", "always");
                        CreateNode(xmlDoc, xeRoot, "priority", "1.0");
    
                        xeRoot = xmlDoc.CreateElement("data");
                        XmlNode xn_url = xmlDoc.SelectSingleNode("urlset").SelectNodes("url").Item(i);
                        if (xn_url != null)
                        {
                            xn_url.AppendChild(xeRoot);
                        }
    
                        xeRoot = xmlDoc.CreateElement("dispaly");
                        XmlNode xn_data = xmlDoc.SelectSingleNode("urlset").SelectNodes("url").Item(i).SelectSingleNode("data");
                        if (xn_data != null)
                        {
                            xn_data.AppendChild(xeRoot);
                        }
    
                        CreateNode(xmlDoc, xeRoot, "innerID", status.List[i].SKUCCId.ToString());
                        CreateNode(xmlDoc, xeRoot, "sourceTime", DateTime.Now.ToLocalTime().ToString());
                        CreateNode(xmlDoc, xeRoot, "productLine", "1");
                        CreateNode(xmlDoc, xeRoot, "isDelete", "0");
                        CreateNode(xmlDoc, xeRoot, "title", @"<![CDATA[" + status.List[i].GoodsName + "]]>");
                        CreateNode(xmlDoc, xeRoot, "poster", @"<![CDATA[]]>");
                        CreateNode(xmlDoc, xeRoot, "originalPrice", status.List[i].PricePure.ToString());
                        CreateNode(xmlDoc, xeRoot, "discountPrice", status.List[i].PricePure.ToString());
    
                        CreateNode(xmlDoc, xeRoot, "onlineTime", "1401353274");
                        CreateNode(xmlDoc, xeRoot, "totalSalesVolume", "100");
                        CreateNode(xmlDoc, xeRoot, "totalReviewCounts", "100");
                        CreateNode(xmlDoc, xeRoot, "goodReviewCounts", "10000");
                        CreateNode(xmlDoc, xeRoot, "rating", "9.6");
                        CreateNode(xmlDoc, xeRoot, "maxRating", "10");
                        CreateNode(xmlDoc, xeRoot, "brand", status.List[i].GoodsName);
                        CreateNode(xmlDoc, xeRoot, "postageFlag", "1");
                        CreateNode(xmlDoc, xeRoot, "certifiedFlag","1");
                        CreateNode(xmlDoc, xeRoot, "compensationFlag","1");
                        CreateNode(xmlDoc, xeRoot, "bestSellerFlag", "1");
                        CreateNode(xmlDoc, xeRoot, "groupSale", "1");
                        CreateNode(xmlDoc, xeRoot, "discountFlag", "0");
                        CreateNode(xmlDoc, xeRoot, "discountStartTime", "");
                        CreateNode(xmlDoc, xeRoot, "discountEndTime", "");
                        CreateNode(xmlDoc, xeRoot, "newProductFlag", "0");
    
                        CreateNode(xmlDoc, xeRoot, "status", "1");
                        CreateNode(xmlDoc, xeRoot, "description", @"<![CDATA[]]>");
    
                        CreateNode(xmlDoc, xeRoot, "type", "配件");
                        CreateNode(xmlDoc, xeRoot, "hot", "80");
                        CreateNode(xmlDoc, xeRoot, "maxHot", "100");
                        CreateNode(xmlDoc, xeRoot, "comments", "Good!");
                        CreateNode(xmlDoc, xeRoot, "model", "");
                        CreateNode(xmlDoc, xeRoot, "origin", "");
                        CreateNode(xmlDoc, xeRoot, "urlForApp", @"<![CDATA[]]>");
                        CreateNode(xmlDoc, xeRoot, "urlForWeb", @"<![CDATA[]]>");
                    }
                }
    
                try
                {
                    xmlDoc.Save("d://商品_非3c商品.xml");
                }
                catch (Exception e)
                {
                    //显示错误信息  
                    Console.WriteLine(e.Message);
                }
                //Console.ReadLine();  
    
            }
            /// <summary>    
            /// 创建节点    
            /// </summary>    
            /// <param name="xmldoc"></param>  xml文档  
            /// <param name="parentnode"></param>父节点    
            /// <param name="name"></param>  节点名  
            /// <param name="value"></param>  节点值  
            ///   
            public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
            {
                XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
                node.InnerText = value;
                parentNode.AppendChild(node);
            }
        }

    效果图:

  • 相关阅读:
    【codeforces 723F】stSpanning Tree
    struts2.0中struts.xml配置文件详解
    存储过程中调用JAVA程序段
    本不该逃避
    利用js实现对页面的自动刷新
    [转]从硬盘安装 RedHat Enterprise Linux Server 5 iso
    正则表达式使用
    利用XmlBean轻松读写xml(转)
    Struts2+Spring2+Hibernate3 web应用示例(七)
    在DWR中实现直接获取一个JAVA类的返回值的两种方法
  • 原文地址:https://www.cnblogs.com/foreverfendou/p/6169184.html
Copyright © 2011-2022 走看看