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);
            }
        }

    效果图:

  • 相关阅读:
    angularjs的$on、$emit、$broadcast
    angularjs中的路由介绍详解 ui-route(转)
    ionic入门教程-ionic路由详解(state、route、resolve)(转)
    Cocos Creator 加载使用protobuf第三方库,因为加载顺序报错
    Cocos Creator 计时器错误 cc.Scheduler: Illegal target which doesn't have uuid or instanceId.
    Cocos Creator 构造函数传参警告 Can not instantiate CCClass 'Test' with arguments.
    Cocos Creator 对象池NodePool
    Cocos Creator 坐标系 (convertToWorldSpaceAR、convertToNodeSpaceAR)
    Cocos Creator 常驻节点addPersistRootNode
    Cocos Creator 配合Tiled地图的使用
  • 原文地址:https://www.cnblogs.com/foreverfendou/p/6169184.html
Copyright © 2011-2022 走看看