直接上代码:

在应用程序中

 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}", , )); if (status.List.Count > )
{
for (int i = ; 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", "");
CreateNode(xmlDoc, xeRoot, "isDelete", "");
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", "");
CreateNode(xmlDoc, xeRoot, "totalSalesVolume", "");
CreateNode(xmlDoc, xeRoot, "totalReviewCounts", "");
CreateNode(xmlDoc, xeRoot, "goodReviewCounts", "");
CreateNode(xmlDoc, xeRoot, "rating", "9.6");
CreateNode(xmlDoc, xeRoot, "maxRating", "");
CreateNode(xmlDoc, xeRoot, "brand", status.List[i].GoodsName);
CreateNode(xmlDoc, xeRoot, "postageFlag", "");
CreateNode(xmlDoc, xeRoot, "certifiedFlag","");
CreateNode(xmlDoc, xeRoot, "compensationFlag","");
CreateNode(xmlDoc, xeRoot, "bestSellerFlag", "");
CreateNode(xmlDoc, xeRoot, "groupSale", "");
CreateNode(xmlDoc, xeRoot, "discountFlag", "");
CreateNode(xmlDoc, xeRoot, "discountStartTime", "");
CreateNode(xmlDoc, xeRoot, "discountEndTime", "");
CreateNode(xmlDoc, xeRoot, "newProductFlag", ""); CreateNode(xmlDoc, xeRoot, "status", "");
CreateNode(xmlDoc, xeRoot, "description", @"<![CDATA[]]>"); CreateNode(xmlDoc, xeRoot, "type", "配件");
CreateNode(xmlDoc, xeRoot, "hot", "");
CreateNode(xmlDoc, xeRoot, "maxHot", "");
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);
}
}

效果图:

C# 生成XML 多级节点的更多相关文章

  1. python lxml库生成xml文件-节点命名空间问题

    lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...

  2. 自动生成XML空节点格式的差异

    我们用C#开发了上位机配置软件,用C开发了嵌入式软件,然后他们之间的参数交互靠XML文件来沟通. C#中添加一个空的节点有以下几种情况. 不给节点的InnerText赋值: <root> ...

  3. C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML

    using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...

  4. java 解析并生成 XML

    在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...

  5. Android 使用xml序列化器生成xml文件

    在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...

  6. Android 生成xml文件

    生成XML文件备份短信,其格式为: <?xml version="1.0" encoding="UTF-8" standalone="true& ...

  7. C# 生成xml文件

    本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...

  8. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  9. php生成xml的四种方法(转)

    <?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...

随机推荐

  1. 如何在MAC机器中实现移动设备WiFI上网(没有专门的无线路由器的情况)

    在很多办公室甚至家中都有无线路由器以方便通过WIFI信号上网.如果没有无线路由器,如何让多个移动智能终端同时上网呢?如果你是Windows用户那么可以选择wifi共享精灵来解决,如果你拥有MAC机器, ...

  2. HTML5--》点击显示隐藏内容

    <details>浏览器支持比较差,可以用JavaScript实现这种功能. <!doctype html> <html> <head> <met ...

  3. CentOS 卸载已安装软件

    根据软件的安装类型,选择合适的卸载方式: 卸载前确定软件的完整的版本号:#rpm -qa ×××*#rpm -ql xxx-xxx  //查找安装目录 执行卸载命令:#rpm -e xxx-xxx # ...

  4. Ninject依赖注入——构造函数的注入

    1.Ninject简介 Ninject是基于.Net平台的依赖注入框架,它能够将应用程序分离成一个个高内聚.低耦合(loosely-coupled, highly-cohesive)的模块,然后以一种 ...

  5. POJ C程序设计进阶 编程题#1:分配病房

    编程题#1:分配病房 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 某个科 ...

  6. CentOS下修复grub引导程序

    1.早上装系统,试了好几次.最终grub修复成功! 解决方法大致如下: 开机遇见 grub>的时候 下面看解决方法 首先是既然插上U盘能进系统  那我们插上U盘 正常进入系统  然后看操作 [r ...

  7. SQL服务器更改名称后

    SQL服务器更改名称后 编写人:CC阿爸 2014-6-15 在日常SQL 2005数据库的操作中,有时安装完成数据库后,再更名,造成部分SQL服务不能正常使用(在SQL2000 时,想都别想更名了) ...

  8. Cron和Spring定时任务

    1.Java Spring spring定时任务cronExpression的值(配置定时时间)格式说明: 一个cronExpression表达式有至少6个(也可能是7个)由空格分隔的时间元素.从左至 ...

  9. redis的lua使用(EVALSHA)

    redis 127.0.0.1:6379> SCRIPT LOAD "local list=redis.call('KEYS', KEYS[1] .. '*') return (tab ...

  10. 图片来自腾讯,未经授权不可引用,js解决方法

    问题记录,解决后来更新 js伪造Referer, 外链图片让用户浏览时,不发送 referer 字段给原网站的方法 A 网站引用了 B 站的 图片 <img src="b_url&qu ...