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}", , )); 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 多级节点的更多相关文章
- python lxml库生成xml文件-节点命名空间问题
lxml库,处理xml很强大,官方文档:https://lxml.de/tutorial.html#namespaces 例如: 我们要生成如下格式的报文: <ttt:jesson xmlns: ...
- 自动生成XML空节点格式的差异
我们用C#开发了上位机配置软件,用C开发了嵌入式软件,然后他们之间的参数交互靠XML文件来沟通. C#中添加一个空的节点有以下几种情况. 不给节点的InnerText赋值: <root> ...
- C#使用Linq To XML读取XML,Linq生成XML,Linq创建带属性或带节点XML
using System; using System.Linq; using System.Xml.Linq; namespace Sample2 { class Program { static v ...
- java 解析并生成 XML
在 java 中使用 Dom4j 解析 XML 对 XML 文件的解析,通常使用的是 Dom4j 和 jdom 作为XML解析工具. 在此只介绍下 Dom4j 对 XML 文件的解析使用方法. 1. ...
- Android 使用xml序列化器生成xml文件
在<Android 生成xml文件>一文中使用流的形式写入xml格式文件,但是存在一定的问题,那就是在短信内容中不能出现<>之类的括号,本文使用xml序列化器来解决 xml序列 ...
- Android 生成xml文件
生成XML文件备份短信,其格式为: <?xml version="1.0" encoding="UTF-8" standalone="true& ...
- C# 生成xml文件
本篇文章旨在.net环境下生成xml文件,以控制台应用程序为例进行说明. 1.在vs中新建控制台应用程序CreateXml 2.CreateXmlFile:主要生成xml的函数 public void ...
- 使用XML序列化器生成XML文件和利用pull解析XML文件
首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...
- php生成xml的四种方法(转)
<?xml version="1.0" encoding="utf-8"?> <article> <item> <ti ...
随机推荐
- ASP.NET MVC 开源项目学习之ProDinner (三)
第四层:Resources 这一层里面主要是几个资源文件. 资源文件知识小杂烩: 几乎每一个生产性应用程序都需要使用资源.资源是在逻辑上由应用程序部署的任何非可执行数据.资源可以在应用程序中作为错 ...
- php实现树状结构无级分类
php实现树状结构无级分类 ).",'树2-1-1-2')";mysql_query($sql);?>
- nodejs安装及环境配置
简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O服务端Ja ...
- Android IOS WebRTC 音视频开发总结(五七)-- 网络传输上的一种QoS方案
本文主要介绍一种QoS的解决方案,文章来自博客园RTC.Blacker,欢迎关注微信公众号blacker,更多详见www.rtc.help QoS出现的背景: 而当网络发生拥塞的时候,所有的数据流都有 ...
- windows上修改路由表
1.查看电脑中的路由的命令: route print 2.修改“metric”,值越小权限越高: route add 0.0.0.0 mask 0.0.0.0 192.168.1.1 metric 5 ...
- C语言开源项目
值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...
- C puzzles详解【31-33题】
第三十一题 The following is a simple C program to read and print an integer. But it is not working proper ...
- (一)、NodeJS (转载)
NodeJS基础 ------ 转载自阿里巴巴 什么是NodeJS JS是脚本语言,脚本语言都需要一个解 ...
- js实现文字字幕滚动
<div class="dggd_r" id="h" style="height:400px;overflow:hidden;display:i ...
- .Net 内存泄露
一.事件引起的内存泄露 1.不手动注销事件也不发生内存泄露的情况 我们经常会写EventHandler += AFunction; 如果没有手动注销这个Event handler类似:EventHan ...