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 ...
随机推荐
- Multithreading annd Grand Central Dispatch on ios for Beginners Tutorial-多线程和GCD的入门教程
原文链接:Multithreading and Grand Central Dispatch on iOS for Beginners Tutorial Have you ever written a ...
- WP_图片管理机制/异步读取网络图片
项目有这样的需求, 要求窗口加载一揽子图片,为了不让UI阻塞太久,采用异步读取后绑定显示的方案. 图片的下载应该采用并发的过程(等待网络响应会很耗时,一张一张的下载,等待时间太长) 图片的下载不能占用 ...
- 新写的c++日志库:log4K
网是开源的c/c++日志库也不少,但用起来总觉得不方便,于是动手写了一个C++日志框架Log4K. 测试代码: #include "log4k.h" #pragma comment ...
- Oracle 通过触发器 来创建 同步临时表 及处理 通过 自治事务 来解决 查询 基表的问题
// 触发器 create or replace trigger tr_sync_BD_MARBASCLASS after INSERT or UPDATE on BD_MARBASCLASS for ...
- ORA-00031: session marked for kill 处理Oracle中杀不掉的锁
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- [Cocos2d-x for WP8学习笔记] HelloWorld结构分析
先来看一下目录结构: Assets:游戏资源文件,图片音频等,Resource文件夹也有类似功能 include:用于放置游戏头文件 Shaders:渲染器着色器文件(大雾) cocos2dorig. ...
- php实现文件上传的源码
php实现文件上传的源码,更多php技术开发就去php教程网,http://php.662p.com <?php ##author :Androidyue ##sina @androidyue ...
- 如何使用CSS3画出一个叮当猫
刚学习了这个案例,然后觉得比较好玩,就练习了一下.然后发现其实也不难,如果你经常使用PS或者Flash的话,应该就会知道画个叮当猫是很容易 的事,至少我是这么觉得.但是,用CSS3画出来确实是第一次接 ...
- HTML中Meta标签大全
在网页的HTML源代码中一个重要的代码“”(即通常所说的META标签).META标签用来描述一个HTML网页文档的属性,例如作者.日期和时间.网页描述.关键词.页面刷新等. 1.META标签的keyw ...
- CSS计数器与动态计数呈现
代码: CSS代码: body { counter-reset: icecream; } input:checked { counter-increment: icecream; } .total:: ...